我正在创建一个简单的C ++项目来与CMake相处,但我做不正确,因此我的应用无法成功构建(链接)(注意:我对当前的CMake =版本3.14感兴趣)。 它是这样组织的命令行计算器:
calculator/
CMakeLists.txt
application/
CMakeLists.txt
src/
...
libraries/
mymath/
CMakeLists.txt
include/
mymath/
mymath.h
src/
...
要构建它,请手动执行:
1 - create static library: libmymath.a (yep, I'm on Linux)
2 - create object file: application.o
3 - link them together into: application (executable)
我已经从cmake.org上阅读了一些官方文档以及一些教程,但是无法用它们解决(顺便说一句,这些教程/帮助中有许多是新的,但是对于旧版本的cmake,如果没有,我可以找不到它们涵盖这种情况,或者实际上从头到尾都没有(这不是很先进,并且需要先有关于做事的知识,例如首先使用自己的功能扩展cmake等)。
我还查找了现有的cmake文件,但只发现了最近写的旧文件:C(例如googletest社区驱动的cmake文件)... 我的问好事π!在哪里?
我当前的CMakeLists.txt编写如下:
# top level CMakeLists.txt
cmake_minimum_required (VERSION 3.14.3)
project (calculator
VERSION 1.0.0
DESCRIPTION "nah"
LANGUAGES CXX)
add_subdirectory(application)
add_subdirectory (library/mymath)
target_link_libraries (calculator-app mymath)
# application/CMakeLists.txt
cmake_minimum_required (VERSION 3.14)
project (calculator-app
VERSION 1.0.0
DESCRIPTION "simple arythmetic command-line calculator"
LANGUAGES CXX)
add_executable (calculator-app)
target_sources (calculator-app PRIVATE
${PROJECT_SOURCE_DIR}/src/main.cpp
${PROJECT_SOURCE_DIR}/src/cli.cpp
${PROJECT_SOURCE_DIR}/src/expression.cpp)
# library/mymath/CMakeLists.txt
cmake_minimum_required (VERSION 3.14)
project (mymath
VERSION 1.0.0
DESCRIPTION "professional prod-ready heavly-opt math impl"
LANGUAGES CXX)
add_library (mymath STATIC)
target_sources(mymath PUBLIC
${PROJECT_SOURCE_DIR}/src/add.cpp
${PROJECT_SOURCE_DIR}/src/sub.cpp
${PROJECT_SOURCE_DIR}/src/mul.cpp
${PROJECT_SOURCE_DIR}/src/div.cpp)
target_include_directories(mymath INTERFACE
${PROJECT_SOURCE_DIR}/include)
目前,它可以正常工作:
# being in calculator/
$ mkdir build
$ cd build
$ cmake ..
然后失败:
$ make
有错误:
Scanning dependencies of target mymath
[ 7%] Building CXX object library/mymath/CMakeFiles/mymath.dir/src/add.cpp.o
[ 15%] Building CXX object library/mymath/CMakeFiles/mymath.dir/src/sub.cpp.o
[ 23%] Building CXX object library/mymath/CMakeFiles/mymath.dir/src/mul.cpp.o
[ 30%] Building CXX object library/mymath/CMakeFiles/mymath.dir/src/div.cpp.o
[ 38%] Linking CXX static library libmymath.a
[ 38%] Built target mymath
Scanning dependencies of target calculator-app
[ 46%] Building CXX object application/CMakeFiles/calculator-app.dir/src/main.cpp.o
[ 53%] Building CXX object application/CMakeFiles/calculator-app.dir/src/cli.cpp.o
[ 61%] Building CXX object application/CMakeFiles/calculator-app.dir/src/expression.cpp.o
[ 69%] Building CXX object application/CMakeFiles/calculator-app.dir/__/library/mymath/src/add.cpp.o
[ 76%] Building CXX object application/CMakeFiles/calculator-app.dir/__/library/mymath/src/sub.cpp.o
[ 84%] Building CXX object application/CMakeFiles/calculator-app.dir/__/library/mymath/src/mul.cpp.o
[ 92%] Building CXX object application/CMakeFiles/calculator-app.dir/__/library/mymath/src/div.cpp.o
[100%] Linking CXX executable calculator-app
/usr/bin/ld: CMakeFiles/calculator-app.dir/src/expression.cpp.o: in function `OperatorsMapping::load()':
expression.cpp:(.text+0x151): undefined reference to `mymath::add(int, int)'
/usr/bin/ld: expression.cpp:(.text+0x18a): undefined reference to `mymath::sub(int, int)'
/usr/bin/ld: expression.cpp:(.text+0x1c3): undefined reference to `mymath::mul(int, int)'
/usr/bin/ld: expression.cpp:(.text+0x1fc): undefined reference to `mymath::div(int, int)'
/usr/bin/ld: expression.cpp:(.text+0x235): undefined reference to `mymath::div(int, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [application/CMakeFiles/calculator-app.dir/build.make:175: application/calculator-app] Error 1
make[1]: *** [CMakeFiles/Makefile2:91: application/CMakeFiles/calculator-app.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我尝试将顶级CMakeLists.txt文件与application /中的其中一个合并,并将库目录移至application /中,但是没有用。
我使用了错误的命令吗?还是可能造成结构不良?或者也许我还在以错误的方式思考cmake?
我很高兴发现这一点,并且我一定会感谢一些很好的教程,这些教程彻底地完成了现代cmake的常见工作:)。