Ask Your Question
0

How to resolve the problem of undefined reference in CMake on linux ?

asked 2018-06-08 10:03:48 -0600

azdoud.y gravatar image

updated 2018-06-08 10:16:59 -0600

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

edit retag flag offensive close merge delete

Comments

that's a linker error. (headers are NOT the problem here)

most probably, you forgot to add a cpp file to

add_executable( main main.cpp ????.cpp )

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 )

berak gravatar imageberak ( 2018-06-08 10:09:26 -0600 )edit

the classe file are within the same dir of main.cpp

/home/azdoud/prototype02

yes it a part of my code I save Mat in my AdFeature object.

azdoud.y gravatar imageazdoud.y ( 2018-06-08 10:20:09 -0600 )edit

ok. still, you have to list every cpp file needed in add_executable

berak gravatar imageberak ( 2018-06-08 10:24:51 -0600 )edit

these are the files of my code

[azdoud@video-processor prototype02]$ ls

Adfeature.cpp Adfeature.h CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt main.cpp Makefile

I set in CMakeLists.txt this conf

   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)
    add_executable( maincode main.cpp Adfeature.cpp Adfeature.h )

but I still have the errors

azdoud.y gravatar imageazdoud.y ( 2018-06-08 10:32:43 -0600 )edit

you also have to run cmake again

berak gravatar imageberak ( 2018-06-08 10:34:38 -0600 )edit

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 !

berak gravatar imageberak ( 2018-06-08 10:38:17 -0600 )edit

I had rerun cmake but I sill have undefined reference even if with this CMakeList

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)
target_include_directories(
        main PUBLIC
        /home/azdoud/prototype02
)
add_executable( maincode main.cpp Adfeature.cpp Adfeature.h )
azdoud.y gravatar imageazdoud.y ( 2018-06-08 10:50:52 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-06-08 11:19:18 -0600

azdoud.y gravatar image

yes, I have 2 add_executable lines there, that was the problem thank you @berak the code run with no problem

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-08 10:03:48 -0600

Seen: 32,474 times

Last updated: Jun 08 '18