Make failing with not used function - c++

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

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

Related

undefined reference compiling taskwarrior on arm chromebook

I tried to build task-2.5.1 on my armv81 chromebook inside a termux session. It didn't work due build dependencies so I installed them then uncompressed and started in a fresh new directory. When I ran make VERBOSE=1 I got an undefined reference to __atomic_fetch_add_4 when linking...
[ 98%] Linking CXX executable lex
cd /data/data/com.termux/files/home/src/task-2.5.1/src && /data/data/com.termux/files/usr/bin/cmake -E cmake_link_script CMakeFiles/lex_executable.dir/link.txt --verbose=1
/data/data/com.termux/files/usr/bin/c++ -Wall -Wsign-compare -Wreturn-type -std=c++11 -stdlib=libc++ -O3 -DNDEBUG -rdynamic CMakeFiles/lex_executable.dir/lex.cpp.o -o lex libtask.a commands/libcommands.a columns/libcolumns.a libtask.a -lgnutls -luuid
/data/data/com.termux/files/usr/bin/arm-linux-androideabi-ld: libtask.a(Nibbler.cpp.o): in function `Nibbler::Nibbler(Nibbler const&)':
Nibbler.cpp:(.text+0x8c): undefined reference to `__atomic_fetch_add_4'
/data/data/com.termux/files/usr/bin/arm-linux-androideabi-ld: libtask.a(Nibbler.cpp.o): in function `Nibbler::operator=(Nibbler const&)':
Nibbler.cpp:(.text+0xe0): undefined reference to `__atomic_fetch_add_4'
/data/data/com.termux/files/usr/bin/arm-linux-androideabi-ld: Nibbler.cpp:(.text+0x104): undefined reference to `__atomic_fetch_add_4'
/data/data/com.termux/files/usr/bin/arm-linux-androideabi-ld: libtask.a(Nibbler.cpp.o): in function `Nibbler::~Nibbler()':
Nibbler.cpp:(.text+0x174): undefined reference to `__atomic_fetch_add_4'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/CMakeFiles/lex_executable.dir/build.make:90: src/lex] Error 1
make[2]: Leaving directory '/data/data/com.termux/files/home/src/task-2.5.1'
make[1]: *** [CMakeFiles/Makefile2:175: src/CMakeFiles/lex_executable.dir/all] Error 2
make[1]: Leaving directory '/data/data/com.termux/files/home/src/task-2.5.1'
make: *** [Makefile:152: all] Error 2
I found this discussion about undefined references in arm builds. I tried adding a -latomic to CMakeFiles/task_executable.dir/link.txt but it didn't change the output.
Any ideas how to fix this?
Many thanks to wbsch in this issue (posted just after this one) for the link to the known fix which was successful on my machine, removing the three lines from CMakeLists.txt.

undefined reference to `omp_get_wtime'

i learn c++, OpenMP. I have tried compile my program with omp_get_wtime and it doesn't work, but when i deleted this, it works. i must add something, but what and where. In openmptryflags or Cmakelists?
snap/clion/61/bin/cmake/linux/bin/cmake --build /home/bapah/CLionProjects/omp1/cmake-build-debug --target omp1 -- -j 6
[ 50%] Linking CXX executable omp1
CMakeFiles/omp1.dir/main.cpp.o: In function `main':
/home/bapah/CLionProjects/omp1/main.cpp:36: undefined reference to `omp_get_wtime'
collect2: error: ld returned 1 exit status
CMakeFiles/omp1.dir/build.make:83: recipe for target 'omp1' failed
make[3]: *** [omp1] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/omp1.dir/all' failed
make[2]: *** [CMakeFiles/omp1.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/omp1.dir/rule' failed
make[1]: *** [CMakeFiles/omp1.dir/rule] Error 2
Makefile:118: recipe for target 'omp1' failed
make: *** [omp1] Error 2
Solution :
add to CMakeList.txt
SET_TARGET_PROPERTIES(omp1 PROPERTIES COMPILE_FLAGS "-fopenmp"
LINK_FLAGS "-fopenmp")
P.s. omp1 - name your project.
P.p.s. also you can write target_link_libraries(omp1 -fopenmp)

I want to know this cause and solution

[ 6%] Built target ChainedLib
[ 12%] Built target ChainedApp
[ 15%] Linking CXX executable ColorConsole
/usr/bin/ld: cannot open output file ColorConsole: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [ColorConsole] 오류 1
make[1]: *** [CMakeFiles/ColorConsole.dir/all] 오류 2
make: *** [all] 오류 2
I install plog library.
cmake CMakeList.txt complete. but, makefile is fail
I don't know cause
You have a directory called ColorConsole. The makefile tries to produce a file with that name. The solution is most likely to remove (or rename) the directory.

Error when trying to get the current process memory size on C++, MinGW

When I try to call GetProcessMemoryInfo I get an error:
undefined reference to `GetProcessMemoryInfo'
I've seen this issue:Undefined reference to getprocessmemoryinfo#12
but it doesn't solve mine.
I'm trying to know what is the size of my process in the RAM so to do that I need to use the 'GetProcessMemoryInfo' method.
My problem is that the link breaks when I do that.
CmakeLists.txt:
project(maxpath)
set(dir ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${dir}/build")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static-libgcc -static-libstdc++ -static -m64 -lpsapi")
file( GLOB LIB_ALG algorithms/*.hpp )
file( GLOB LIB_DS datastructures/*.hpp )
file( GLOB LIB_LOG include/*.h)
set(GRID_GENERATOR
${LIB_ALG}
${LIB_DS}
${LIB_LOG}
grid/generator.cpp
grid/grid.hpp)
set(GRID_SOLVER
${LIB_ALG}
${LIB_DS}
${LIB_LOG}
grid/main_grid.cpp
grid/grid.hpp
include/memory_helper.cpp include/memory_helper.hpp include/fnv.h)
add_executable(gridGenerator ${GRID_GENERATOR})
add_executable(gridSolver ${GRID_SOLVER})
You can see that I use the -lpsapi argument,
The error I get is:
[ 33%] Linking CXX executable "some path...\gridSolver.exe"
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0xf1): undefined reference to `GetProcessMemoryInfo'
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0x131): undefined reference to `GetProcessMemoryInfo'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj): bad reloc address 0x0 in section `.pdata'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [some path.../gridSolver.exe] Error 1
CMakeFiles\gridSolver.dir\build.make:121: recipe for target 'some path.../gridSolver.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/gridSolver.dir/all] Error 2
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/gridSolver.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/gridSolver.dir/rule] Error 2
CMakeFiles\Makefile2:115: recipe for target 'CMakeFiles/gridSolver.dir/rule' failed
mingw32-make.exe: *** [gridSolver] Error 2
makefile:130: recipe for target 'gridSolver' failed
I'm working with CLion and mingw-w64\x86_64-4.8.3-posix-seh-rt_v3-rev2\mingw64
Is there another way of doing this (except of using psapi)?
The answer there is correct. You should link against psapi:
FIND_LIBRARY (PSAPI Psapi)
TARGET_LINK_LIBRARIES(gridSolver ${PSAPI})
TARGET_LINK_LIBRARIES(gridGenerator ${PSAPI})
Or you can add it manually to the linker flags - you added it to the compiler flags in your example.

CMake fails compilation

I am trying to compile cocos2d-x 3.5 on openSUSE 13.2 and i get the following error in .log file:
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTryCompileExec846787943/fast"
/usr/bin/gmake -f CMakeFiles/cmTryCompileExec846787943.dir/build.make CMakeFiles/cmTryCompileExec846787943.dir/build
gmake[1]: Entering directory '/home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec846787943.dir/CheckSymbolExists.c.o
/usr/bin/cc -fno-exceptions -std=c99 -o CMakeFiles/cmTryCompileExec846787943.dir/CheckSymbolExists.c.o -c /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTryCompileExec846787943
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec846787943.dir/link.txt --verbose=1
/usr/bin/cc -fno-exceptions -std=c99 CMakeFiles/cmTryCompileExec846787943.dir/CheckSymbolExists.c.o -o cmTryCompileExec846787943 -rdynamic
CMakeFiles/cmTryCompileExec846787943.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x16): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/cmTryCompileExec846787943.dir/build.make:88: recipe for target 'cmTryCompileExec846787943' failed
gmake[1]: *** [cmTryCompileExec846787943] Error 1
gmake[1]: Leaving directory '/home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp'
Makefile:118: recipe for target 'cmTryCompileExec846787943/fast' failed
gmake: *** [cmTryCompileExec846787943/fast] Error 2
File /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTryCompileExec3838281423/fast"
/usr/bin/gmake -f CMakeFiles/cmTryCompileExec3838281423.dir/build.make CMakeFiles/cmTryCompileExec3838281423.dir/build
gmake[1]: Entering directory '/home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec3838281423.dir/CheckFunctionExists.c.o
/usr/bin/cc -fno-exceptions -std=c99 -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTryCompileExec3838281423.dir/CheckFunctionExists.c.o -c /usr/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTryCompileExec3838281423
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec3838281423.dir/link.txt --verbose=1
/usr/bin/cc -fno-exceptions -std=c99 -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTryCompileExec3838281423.dir/CheckFunctionExists.c.o -o cmTryCompileExec3838281423 -rdynamic -lpthreads
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
CMakeFiles/cmTryCompileExec3838281423.dir/build.make:88: recipe for target 'cmTryCompileExec3838281423' failed
gmake[1]: *** [cmTryCompileExec3838281423] Error 1
gmake[1]: Leaving directory '/home/humman/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build/CMakeFiles/CMakeTmp'
Makefile:118: recipe for target 'cmTryCompileExec3838281423/fast' failed
gmake: *** [cmTryCompileExec3838281423/fast] Error 2
I installed the dependencies and libraries manually, but I still get this error. Does anyone know whats going on?
Note: when i try to compile, the first 2 lines are:
~/android-dev/cocos2d-x-3.5/cocos2d-x-3.5/build/linux-build> cmake ../..
CMake Error at CMakeLists.txt:35 (cmake_policy):
Policy "CMP0054" is not known to this version of CMake.
Thanks :D
Solved, I just did comment this lines out in the CMakeList.txt file
#if(CMAKE_VERSION VERSION_GREATER 3)
#cmake_policy(SET CMP0054 NEW)
#endif()
but now, when I use make command (No matter where, using NetBeans or terminal) it ends with this error at 98% (after about 15mins compiling)
[ 98%] Built target cocos2d
[ 98%] Built target MyGame_CORE_PRE_BUILD
MyGame_PRE_BUILD ...
[ 98%] Built target MyGame_PRE_BUILD
Linking CXX executable bin/MyGame
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: lib/libcocos2d.a(CCSpriteFrameCache.cpp.o): undefined reference to symbol '_ZSt28_Rb_tree_rebalance_for_erasePSt18_Rb_tree_node_baseRS_##GLIBCXX_3.4'
/usr/lib64/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/MyGame.dir/build.make:164: recipe for target 'bin/MyGame' failed
make[2]: *** [bin/MyGame] Error 1
CMakeFiles/Makefile2:64: recipe for target 'CMakeFiles/MyGame.dir/all' failed
make[1]: *** [CMakeFiles/MyGame.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
Any idea??
Solved, If anyone has the same problem, just use cmake with the argument -pthread and then you will be able to build using make. If you wanna build using NetBeans, u have to leave only the -G "Unix..." argument and add in the CMakefile.txt the line: set(-pthread) --I did add it at the top-- and thats all, it should be everything ok :D cheers!
This appears to be a long-standing CMake bug. Something else is going wrong, CMake gets confused, and reports this spurious problem instead of the real error.
Look for "thread" in your CMakeLists.txt file and temporarily remove that.