I am trying to build some mixed C/C++ code to run some simple test code (HelloTest) as such:
#include
#include "cpp_header.h"
extern "C"
{
#include "c_header.h"
}
using namespace std;
int main(int argc, char **argv) {
cout << "Hello, World!" << endl;
return 0;
}
but I get this very strange error:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/user/Library/Caches/clion10/cmake/generated/c1d0f54d/c1d0f54d/Debug --target all -- -j 2
make[2]: *** No rule to make target `/path/to/uthash/utarray.h', needed by `HelloTest'. Stop.
make[1]: *** [CMakeFiles/HelloTest.dir/all] Error 2
make: *** [all] Error 2
Now, utarray.h is part of uthash (http://troydhanson.github.io/uthash/), and savvy readers will know that this is not a question of the .c/cpp missing - utarray.h doesn't have one.
The CMake file -ish looks like this
cmake_minimum_required(VERSION 3.2)
project(HelloTest)
# CPP stuff
find_package(CPP_PACKAGE REQUIRED)
include_directories(${CPP_PACKAGE_INCLUDE_DIR})
link_directories(${CPP_PACKGE_LIB_DIR})
# C Headers
set(C_ROOT_DIR "/path/to/c/library")
set(C_INCLUDE_DIR ${C_ROOT_DIR}/src
${C_ROOT_DIR}/ext/mtrand
${C_ROOT_DIR}/ext/uthash/src)
include_directories(${C_INCLUDE_DIR})
# C Headers & Source [we want to avoid globbing]
set(C_SOURCE ${C_ROOT_DIR}/src/bnp.c
${C_ROOT_DIR}/src/bnp.h
.
.
.
${C_ROOT_DIR}/ext/uthash/utarray.h
${C_ROOT_DIR}/ext/uthash/uthash.h
${C_ROOT_DIR}/ext/uthash/utlist.h
${C_ROOT_DIR}/ext/uthash/utstring.h)
link_directories(${C_SOURCE})
set(SOURCE_FILES main.cpp)
add_executable(HelloTest ${SOURCE_FILES})
target_link_libraries(HelloTest ${CPP_LIBRARIES} ${C_SOURCE})
解决方案
You set C_SOURCE variable to list of sources and headers, but use it in commands link_directories and target_link_libraries, related to linking, not a compiling.
Probably, you need to append content of this variable to SOURCE_FILES instead.