4

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. 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.

I have tried looking online how to properly use cmake, but I can't find a whole lot.

Here is what i have in my 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(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...

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

anacy
  • 399
  • 4
  • 14

1 Answers1

5

It seems you have an unmentioned dependency between your libraries.

Your deckofcards library uses stuff from the card library. You need to tell this to CMake, otherwise it won't be able to link your program correctly. The correct way to do this is by changing the last two lines of your CMakeLists to

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. 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.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166