0

I am trying to build my code. After I do cmake .. from a build directory I do make -j8 and I get

[ 90%] Building CXX object common/CMakeFiles/common.dir/src/utils/path_util.cpp.o
[ 95%] Linking CXX executable myproj
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
main.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/myproj.dir/build.make:95: recipe for target 'myproj' failed
make[2]: *** [myproj] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myproj.dir/all' failed
make[1]: *** [CMakeFiles/myproj.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

The curious thing is that nowhere in the code I use cv::String.

I have also put

#Search for dependencies
set(MIN_OPENCV_VERSION "3.4.11" CACHE STRING "OpenCV version")
find_package(OpenCV ${MIN_OPENCV_VERSION} REQUIRED
   COMPONENTS core
   PATHS /usr/local/opencv-${MIN_OPENCV_VERSION}
   NO_DEFAULT_PATH
)

in several CMakeLists.txt files and cmake finds opencv

What could be the problem?

EDIT

I set the VERBOSE environment variable to 1 as stated here

and I got

[ 90%] Building CXX object common/CMakeFiles/common.dir/src/utils/path_util.cpp.o
cd /home/user/ws/src/build/common && /usr/bin/c++   -I/usr/local/include/eigen3 -isystem /usr/local/opencv-3.4.11/include -isystem /usr/local/opencv-3.4.11/include/opencv -I/home/user/ws/src/common/include -I/home/user/ws/src/common/src -isystem /usr/local  -fPIC   -o CMakeFiles/common.dir/src/utils/path_util.cpp.o -c /home/user/ws/src/common/src/utils/path_util.cpp
[ 95%] Linking CXX executable road_info
/usr/bin/cmake -E cmake_link_script CMakeFiles/myproj.dir/link.txt --verbose=1
/usr/bin/c++    -rdynamic CMakeFiles/myproj.dir/main.cpp.o  -o myproj mainpub_lib/mainpub.a 
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
main.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/myproj.dir/build.make:95: recipe for target 'myproj' failed
make[2]: *** [myproj] Error 1
make[2]: Leaving directory '/home/user/ws/src/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myproj.dir/all' failed
make[1]: *** [CMakeFiles/myproj.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 5
    Are you sure it's not even used indirectly. That does look you're missing `target_link_libraries`. Given the info in the question we can neither see info about how you're linking OpenCV nor any info about what unexpected things may be happening in your `main` function. It may also be useful to see the command line usage of the linker. Build with `cmake --build . --verbose` and post the linker command printed to the console. – fabian Jun 15 '22 at 14:19
  • c++ is great at hiding from the programmer what is really being used. – n. m. could be an AI Jun 15 '22 at 14:21
  • I have searched for that with the search function in vscode. In any case I have find the opencv package as annotated above. Do I have to put something more in the `CMakeLists.txt` file? – KansaiRobot Jun 15 '22 at 14:22
  • @fabian Thanks!. Thata cmake command, doesn't have to be `..` instead o `.`? – KansaiRobot Jun 15 '22 at 14:24
  • @fabian `unknown argument --verbose` – KansaiRobot Jun 15 '22 at 14:27
  • `cmake --build` runs on a project that was already confingured. The option passed after `--build` is the path to the binary dir you chose. Since you were using `make -j8`, I assumed your current working dir was the dir where you set up the cmake project which would mean the path to pass is `.`, see https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-a-project Which version of cmake are you using? The `--verbose` option should be available since 3.14. You may be able to get the extended output even before by setting the `VERBOSE` environment variable... – fabian Jun 15 '22 at 14:37
  • I set that variable and posted the output – KansaiRobot Jun 15 '22 at 14:41
  • 1
    `/usr/bin/c++ -rdynamic CMakeFiles/myproj.dir/main.cpp.o -o myproj mainpub_lib/mainpub.a` Your `mainpub.a` lib is the only thing that's linked in addition to the `.o` for the main. I don't know why exactly an instance of `cv::String` is created, but almost certainly is a instance created. This could be something hidden like a defaulted parameter. No way to tell without seeing `main.cpp`. Perhaps even the compiler options for this file may be required for understanding this issue. Do you expect a different type named `String` to be used in the `main`? – fabian Jun 15 '22 at 14:51
  • I found out that the problem is not in `main.cpp` but in a hpp file that this file calls. This `one.hpp` file is part of the `mainpub.a` and has a include to a particular file part of that too. What strikes me strange is that `mainpub.a` gets build ok. I would suppose that if the problem is in one of their files, the library would have problems not the `main.cpp`... – KansaiRobot Jun 15 '22 at 15:33
  • I think I solved this by including `target_link_libraries` but it is midnight here, so I ll write about it tomorrow zzz – KansaiRobot Jun 15 '22 at 15:38

1 Answers1

0

First, thanks @fabian for the help and pointers

I finally realized that the problem was not in my main.cpp but in a hpp file that main calls. This one.hpp file included another hpp file that was the one that caused the problem (When I commented it, the problem disapeared)

So what I did was change the CMakeLists.txt of the second level (the one dealing with the problematic hpp file) and added

target_link_libraries(mainpub PRIVATE ${OpenCV_LIBS})

With this the problem was solved

To allow for a verbose make I did export VERBOSE=1 because my make version is old. With the verbose output I could see that mainpub was the only library linked

What strikes me strange is that mainpub was apparently being built even with the problem instead the main build was signaled as problematic

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150