【发布时间】:2021-08-14 19:06:14
【问题描述】:
CMake Error at CMakeLists.txt:12 (target_include_directories):
Cannot specify include directories for target "myproject" which is
not built by this project.
-- Configuring incomplete, errors occurred!
See also "/home/user/project-path/build/CMakeFiles/CMakeOutput.log".
See also "/home/user/project-path/build/CMakeFiles/CMakeError.log".
这个错误似乎是由CMakeLists.txt 中的这一行引起的。删除此行“修复”了问题。
完全披露:我真的不知道这条线是做什么的,或者是否需要。
set(CMAKE_CXX_STANDARD_REQUIRED True)
这是我CMakeLists.txt的全部内容:
cmake_minimum_required(VERSION 3.7)
project(myproject VERSION 1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_MODULE_PATH /home/user/project-path)
configure_file(version.hpp.in version.hpp)
target_include_directories(myproject PUBLIC "${PROJECT_BINARY_DIR}")
find_package(SDL2 REQUIRED)
find_package(SDL2TTF REQUIRED)
include_directories(myproject ${SDL2_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(myproject ${SOURCE_FILES})
target_link_libraries(myproject ${SDL2_LIBRARIES} ${SDL2TTF_LIBRARY} fontconfig)
这是来自文件CMakeError.log的一些相关内容
Run Build Command(s):/usr/bin/gmake cmTC_3abe6/fast && /usr/bin/gmake -f CMakeFiles/cmTC_3abe6.dir/build.make CMakeFiles/cmTC_3abe6.dir/build
gmake[1]: Entering directory '/home/.../build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_3abe6.dir/src.c.o
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_3abe6.dir/src.c.o -c /home/.../build/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_3abe6
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_3abe6.dir/link.txt --verbose=1
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_3abe6.dir/src.c.o -o cmTC_3abe6
/usr/bin/ld: CMakeFiles/cmTC_3abe6.dir/src.c.o: in function `main':
src.c:(.text+0x2f): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x3b): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x47): undefined reference to `pthread_cancel'
/usr/bin/ld: src.c:(.text+0x58): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
gmake[1]: *** [CMakeFiles/cmTC_3abe6.dir/build.make:106: cmTC_3abe6] Error 1
gmake[1]: Leaving directory '/home/.../build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:140: cmTC_3abe6/fast] Error 2
Source file was:
#include <pthread.h>
void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_cancel(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/.../build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/gmake cmTC_f3bc6/fast && /usr/bin/gmake -f CMakeFiles/cmTC_f3bc6.dir/build.make CMakeFiles/cmTC_f3bc6.dir/build
gmake[1]: Entering directory '/home/.../build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_f3bc6.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_f3bc6.dir/CheckFunctionExists.c.o -c /usr/share/cmake-3.18/Modules/CheckFunctionExists.c
Linking C executable cmTC_f3bc6
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f3bc6.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_f3bc6.dir/CheckFunctionExists.c.o -o cmTC_f3bc6 -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
gmake[1]: *** [CMakeFiles/cmTC_f3bc6.dir/build.make:106: cmTC_f3bc6] Error 1
gmake[1]: Leaving directory '/home/.../build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:140: cmTC_f3bc6/fast] Error 2
我知道这段代码是为了测试这个系统是否支持posix线程,它们应该(?)是......
是什么导致了这个问题?
【问题讨论】:
-
所以错误信息是由
add_executable(myproject ${SOURCE_FILES})和target_include_directories(myproject PUBLIC "${PROJECT_BINARY_DIR}")之间的顺序不正确引起的。其他一切都无关紧要,包括CMAKE_CXX_STANDARD_REQUIRED变量的设置和configure_file的位置。