简体   繁体   English

如何配置CMakeLists.txt进行C ++项目?

[英]How To Configure CMakeLists.txt To Make C++ Project?

I am currently trying to teach myself cmake, and I am getting errors when I run make, but the program runs just fine when I compile the files myself. 我目前正在尝试自学cmake,当我运行make时遇到错误,但是当我自己编译文件时程序运行得很好。 In my project, I have 2 header files declaring classes Card and DeckOfCards, 2 .cpp files defining my classes, and a .cpp driver program, main.cpp. 在我的项目中,我有2个头文件声明类Card和DeckOfCards,2 .cpp文件定义我的类,以及.cpp驱动程序main.cpp。

I have tried looking online how to properly use cmake, but I can't find a whole lot. 我试过在线查看如何正确使用cmake,但我找不到很多。

Here is what i have in my CMakeLists.txt... 这是我在CMakeLists.txt中的内容......

cmake_minimum_required(VERSION 2.8.7)
project(Assignment_8)
add_library(card Card.h Card.cpp)
add_library(deckofcards DeckOfCards.h DeckOfCards.cpp)
add_executable(card_class_driver main.cpp)
target_link_libraries(card_class_driver card)
target_link_libraries(card_class_driver deckofcards)

And on the 'target_link_libraries', i have also tried just one 'declaration' if you will, and get the same errors, like this.. 在'target_link_libraries'上,如果你愿意,我也尝试了一个'声明',并得到相同的错误,就像这样..

target_link_libraries(card_class_driver card deckofcards)

I have the source files in a directory called 'assignment_8', and a subdirectory/build directory '_build'... The way I have read, and have been trying it, is in the subdirectory '_build' i've been running "cmake .." ... Everything is fine there.. It says it detects everything.. but then when i run 'make', i get these errors... 我有一个名为'assignment_8'的目录中的源文件,以及一个子目录/ build目录'_build'...我读过的方式,并且一直在尝试它,在子目录'_build'我一直在运行“ cmake ..“......那里的一切都很好..它说它能检测到一切......但是当我运行'make'时,我得到了这些错误......

Scanning dependencies of target card
[ 33%] Building CXX object CMakeFiles/card.dir/Card.cpp.o
Linking CXX static library libcard.a
[ 33%] Built target card
Scanning dependencies of target deckofcards
[ 66%] Building CXX object CMakeFiles/deckofcards.dir/DeckOfCards.cpp.o
Linking CXX static library libdeckofcards.a
[ 66%] Built target deckofcards
Scanning dependencies of target card_class_driver
[100%] Building CXX object CMakeFiles/card_class_driver.dir/main.cpp.o
Linking CXX executable card_class_driver
libdeckofcards.a(DeckOfCards.cpp.o): In function `DeckOfCards::DeckOfCards()':
DeckOfCards.cpp:(.text+0x49): undefined reference to `Card::Card(int, int)'
libdeckofcards.a(DeckOfCards.cpp.o): In function `DeckOfCards::print_deck() const':
DeckOfCards.cpp:(.text+0x238): undefined reference to `Card::print_card() const'
collect2: error: ld returned 1 exit status
make[2]: *** [card_class_driver] Error 1
make[1]: *** [CMakeFiles/card_class_driver.dir/all] Error 2
make: *** [all] Error 2

Any help would be greatly appreciated 任何帮助将不胜感激

It seems you have an unmentioned dependency between your libraries. 您的库之间似乎没有未提及的依赖关系。

Your deckofcards library uses stuff from the card library. 您的deckofcards库使用card库中的内容。 You need to tell this to CMake, otherwise it won't be able to link your program correctly. 你需要告诉CMake,否则它将无法正确链接你的程序。 The correct way to do this is by changing the last two lines of your CMakeLists to 正确的方法是将CMakeLists的最后两行更改为

target_link_libraries(deckofcards card)
target_link_libraries(card_class_driver deckofcards)

Notice that you don't need to specify the card library again on the last line. 请注意,您不需要在最后一行再次指定card库。 CMake automatically resolves transitive dependencies in this case: Since deckofcards depends on card , everything that uses the former will also be linked against the latter. 在这种情况下,CMake会自动解决传递依赖:由于deckofcards依赖于card ,所以使用前者的所有内容也将与后者相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM