–The CXX compiler identification is unknown CMake Error at CMakeLists.txt:4 (project): No CMAKE_CXX_COMPILER could be found. Tell CMake where to find the compiler by setting either the environment variable “CXX” or the CMake cache entry CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH. –Configuring incomplete, errors occurred!
Scanning dependencies of target Tutorial [ 50%] Building C object CMakeFiles/Tutorial.dir/tutorial.c.o [100%] Linking C executable Tutorial CMakeFiles/Tutorial.dir/tutorial.c.o: In function ‘main’: tutorial.c:(.text+0x66): undefined reference to ‘sqrt’ collect2: error: ld returned 1 exit status CMakeFiles/Tutorial.dir/build.make:94: recipe for target ‘Tutorial’ failed
可以清晰的看到是链接时出错,我们代码使用了<math.h>,所以还需要链接它的库:
1 2
# link libraries target_link_libraries(Tutorial m)
还可以添加版本号,所以第一个完整的可以运行的CMakeLists.txt如下:
1 2 3 4 5 6 7 8 9 10 11
# set the cmake version cmake_minimum_required(VERSION 3.5)
# set the project name project(tutorial VERSION 0.0.1 LANGUAGES C)
# add the executable add_executable(Tutorial tutorial.c)
# link libraries target_link_libraries(Tutorial m)