How to resolve the problem of undefined reference in CMake on linux ?
Hi,
I've written a code and I've used classes, however when I'm trying to compile the code using CMake I got these errors
[azdoud@video-processor prototype02]$ cmake -DCMAKE_CXX_FLAGS="-std=c++11"
-- Configuring done
-- Generating done
-- Build files have been written to: /home/azdoud/prototype02
[azdoud@video-processor prototype02]$ make
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
CMakeFiles/main.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x60e): undefined reference to `AdFeature::AdFeature(int, int, bool, cv::Mat)'
main.cpp:(.text+0x668): undefined reference to `AdFeature::~AdFeature()'
main.cpp:(.text+0xf19): undefined reference to `AdFeature::~AdFeature()'
CMakeFiles/main.dir/main.cpp.o: In function `ProcessingSimilarty(cv::VideoCapture, std::vector<AdFeature, std::allocator<AdFeature> >, double, seekStep, std::string)':
main.cpp:(.text+0x13f8): undefined reference to `AdFeature::get_frame()'
main.cpp:(.text+0x170b): undefined reference to `AdFeature::set_found(bool)'
main.cpp:(.text+0x1731): undefined reference to `AdFeature::set_indexLocationAD(int)'
main.cpp:(.text+0x1750): undefined reference to `AdFeature::~AdFeature()'
CMakeFiles/main.dir/main.cpp.o: In function `cleanAdFeature(std::vector<AdFeature, std::allocator<AdFeature> >)':
main.cpp:(.text+0x1b77): undefined reference to `AdFeature::set_found(bool)'
main.cpp:(.text+0x1b97): undefined reference to `AdFeature::set_indexLocationAD(int)'
CMakeFiles/main.dir/main.cpp.o: In function `findAdLocation(std::vector<AdFeature, std::allocator<AdFeature> >, int, double, double)':
CMakeFiles/main.dir/main.cpp.o: In function `void __gnu_cxx::new_allocator<AdFeature>::destroy<AdFeature>(AdFeature*)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorI9AdFeatureE7destroyIS1_EEvPT_[_ZN9__gnu_cxx13new_allocatorI9AdFeatureE7destroyIS1_EEvPT_]+0x18): undefined reference to `AdFeature::~AdFeature()'
CMakeFiles/main.dir/main.cpp.o: In function `void std::_Destroy<AdFeature>(AdFeature*)':
main.cpp:(.text._ZSt8_DestroyI9AdFeatureEvPT_[_ZSt8_DestroyI9AdFeatureEvPT_]+0x14): undefined reference to `AdFeature::~AdFeature()'
collect2: error: ld returned 1 exit status
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
even though adding include_directories (dir of the code) in CMakeLists.txt did not work
cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
include_directories(/home/azdoud/prototype02)
So, how Can I make these .h file a definedable reference?
thank you
that's a linker error. (headers are NOT the problem here)
most probably, you forgot to add a cpp file to
see, where the code for the
AdFeature
class is(all assuming, that this is part of your codebase. if it's not so, it needs the name of the resp. library in
target_link_libraries
)the classe file are within the same dir of main.cpp
yes it a part of my code I save Mat in my AdFeature object.
ok. still, you have to list every cpp file needed in
add_executable
these are the files of my code
Adfeature.cpp Adfeature.h CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt main.cpp Makefile
I set in CMakeLists.txt this conf
but I still have the errors
you also have to run cmake again
pro tip: mark your code with mouse, and press ctrl-k here, to format it nicely
you also got 2 add_executable lines there, bad !
I had rerun cmake but I sill have undefined reference even if with this CMakeList