CMake编译错误:ld返回1退出状态

时间:2019-06-16 04:25:34

标签: c++ cmake compiler-errors

我正在尝试使用CMake构建一个C ++简单项目,将实用程序代码与其余代码分开。

当我包含源文件(My.cpp)时它起作用,但是当我尝试按应有的标题(My.hpp)时却不起作用。

源代码树

cpp-test
...
├── CMakeLists.txt
└── src
    ├── a
    │   └── b
    │       ├── c
    │       │   ├── d
    │       │   │   ├── Dijkstra.cpp
...
    ├── lib
    │   └── my
    │       ├── CMakeLists.txt
    │       ├── My.cpp
    │       └── My.hpp

CMakeLists.txt文件中我缺少什么?

CMakeLists.txt(src / lib / my)

set(MY_LIB_FILES My.hpp My.cpp)
add_library(my ${MY_LIB_FILES})

CMakeLists.txt(。)

set(SOURCE_FILES src/a/b/c/d/Dijkstra.cpp)

cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 14)

set(CMAKE_VERBOSE_MAKEFILE ON)
set_property(GLOBAL PROPERTY RULE_MESSAGES OFF)

project(cpp_test)

include_directories(src/lib/my)
link_directories(src/lib/my)
add_subdirectory(src/lib/my)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
add_dependencies(${PROJECT_NAME} my)
target_link_libraries(${PROJECT_NAME} my)

构建输出

====================[ Build | cpp_test | Debug ]================================
cmake --build ../cmake-build-debug --target cpp_test -- -j 2
[ 50%] Built target my
Scanning dependencies of target cpp_test
CMakeFiles/cpp_test.dir/src/a/b/c/d/Dijkstra.cpp.o: In function `main':
/cpp-test/src/a/b/c/d/Dijkstra.cpp:280: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > my::str<unsigned int>(unsigned int const*, int, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/cpp_test.dir/build.make:80: recipe for target 'cpp_test' failed
make[3]: *** [cpp_test] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/cpp_test.dir/all' failed
make[2]: *** [CMakeFiles/cpp_test.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/cpp_test.dir/rule' failed
make[1]: *** [CMakeFiles/cpp_test.dir/rule] Error 2
Makefile:118: recipe for target 'cpp_test' failed
make: *** [cpp_test] Error 2

0 个答案:

没有答案