Undefined reference when trying to compile a code with cmake - c++

I'm trying to understand what's wrong with my cmake setup. I downloaded the code described in http://alexott.net/en/cpp/BoostAsioProxy.html. It's an asyncrhonous http proxy server using boost.
These are the contents of my CMakeLists.txt:
cmake_minimum_required(VERSION 2.4)
PROJECT(asio-proxy-async)
# Usage:
# cmake . -DCMAKE_INCLUDE_PATH=~/exp/include -DCMAKE_LIBRARY_PATH=~/exp/lib
#
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET (CMAKE_MODULE_PATH ${cpptests_SOURCE_DIR}/cmake CACHE PATH "local cmake")
ADD_DEFINITIONS(-g -Wall -ansi -Wno-deprecated)
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREAD ON)
FIND_PACKAGE(Boost 1.49.0 REQUIRED COMPONENTS filesystem system thread regex)
MESSAGE(STATUS "** Boost Include: ${Boost_INCLUDE_DIR}")
MESSAGE(STATUS "** Boost Libraries: ${Boost_LIBRARIES}")
IF(Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ENDIF(Boost_FOUND)
SET(USED_LIBS ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} ${Boost_REGEX_LIBRARY})
ADD_EXECUTABLE(asio-proxy-async proxy.cpp proxy-server.cpp proxy-conn.cpp)
TARGET_LINK_LIBRARIES(asio-proxy-async ${USED_LIBS})
After I type cmake . the 3 source files compile just ok, but then in the linking phase, I get this:
Linking CXX executable asio-proxy-async
/usr/bin/cmake -E cmake_link_script CMakeFiles/asio-proxy-async.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/asio-proxy-async.dir/proxy.o CMakeFiles/asio-proxy-async.dir/proxy-server.o CMakeFiles/asio-proxy-async.dir/proxy-conn.o -o asio-proxy-async -rdynamic -lboost_system-mt -lboost_thread-mt -lboost_regex-mt
CMakeFiles/asio-proxy-async.dir/proxy.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy.o: In function `error_code':
/usr/local/include/boost/system/error_code.hpp:315: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy.o: In function `boost::asio::error::get_system_category()':
/usr/local/include/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy.o: In function `thread_exception':
/usr/local/include/boost/thread/exceptions.hpp:49: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy.o: In function `condition_error':
/usr/local/include/boost/thread/exceptions.hpp:82: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy-server.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
CMakeFiles/asio-proxy-async.dir/proxy-conn.o: In function `__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make[2]: *** [asio-proxy-async] Error 1
make[2]: Leaving directory `/home/nelsonrp/workspace/boost-test/asio-proxy-async'
make[1]: *** [CMakeFiles/asio-proxy-async.dir/all] Error 2
make[1]: Leaving directory `/home/nelsonrp/workspace/boost-test/asio-proxy-async'
make: *** [all] Error 2
I've seen a couple of posts here in SO talking about this kind of problems with cmake and boost, none of them with final answers though. To clarify things a bit more, let me just point out that if I do:
g++ -g -Wall -c proxy.cpp
g++ -g -Wall -c proxy-conn.cpp
g++ -g -Wall -c proxy-server.cpp
g++ proxy.o proxy-server.o proxy-conn.o -o asio-proxy-async -lboost_system -lboost_thread -lboost_regex -lboost_filesystem
The source compiles just fine, which means that I have boost installed and it is in the right place, the problem just seems to be with cmake. Any suggestions?

The source code archive available on the page ships with its own outdated version of the FindBoost.cmake module. Remove the outdated module file, whose path is asio-proxy-async/cmake/FindBoost.cmake, then re-create your build folder and run cmake again. This will make CMake use the standard FindBoost module which should have no problems finding your existing Boost installation.

Related

dl libs and pthreads required in GitHub Actions

I'm developing a dynamic C++17 library that uses SQLite. It's a CMake project and the requirement for it to be dynamic comes because I plan to mainly use the library through JNI.
So, in my project I build SQLite as a static library:
add_library(sqlite3 STATIC "${sqlite3_SOURCE_DIR}/sqlite3.c") # STATIC to efficiently link to SHARED main library
set_target_properties(sqlite3 PROPERTIES POSITION_INDEPENDENT_CODE ON) # Required to link STATIC to SHARED
target_include_directories(sqlite3 PUBLIC "${sqlite3_SOURCE_DIR}")
And my main library as shared:
add_library(${PROJECT_NAME} SHARED <library sources>)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) # To include headers without using relative paths
target_link_libraries(${PROJECT_NAME} PRIVATE sqlite3)
Then if I link my library to some executable like this it builds and runs absolutely fine on my PC:
add_executable(app <app's sources>)
target_compile_features(app PUBLIC cxx_std_17)
target_include_directories(app PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} <main library src dir>) # Including the main library like this is a temporary solution, because there is no separate include dir yet
target_link_libraries(app PUBLIC ${PROJECT_NAME})
But when I try to build it in GitHub Actions I get these errors:
[100%] Linking CXX executable app
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_create'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlopen'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlclose'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlerror'
/usr/bin/ld: ../src/mylib.so: undefined reference to `dlsym'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_join'
/usr/bin/ld: ../src/mylib.so: undefined reference to `pthread_mutexattr_init'
collect2: error: ld returned 1 exit status
I can fix it by linking the library target to ${CMAKE_DL_LIBS} and Threads::Threads, but I'm not sure if I'm doing what should be done and not just silencing some other mistakes of mine.
To be clear, I don't use any of the functions mentioned in the errors above in the code of my library.
The questions are:
Why does the original code work on my PC and doesn't on GitHub Actions?
Is what I did the right way to fix this?
I'm building the project locally on Ubuntu 22.04 (WSL) with a compiler identified as GNU 11.3.0, while GitHub Actions runs Ubuntu 20.04 using GNU 9.4.0 compiler.

linked library is valid in CMakeLists, but doesn't link at compile time

I'm just starting around with messing around with vulkan, and GLFW, but when I try to compile a test program, it gives me a bunch of linker errors:
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)
find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})
add_executable(vulkan_test main.cpp loops.cpp)
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
Any idea why this is happening? I have both vulkan and GLFW installed, libglfw.so is in my /usr/lib/ directory, and clangd doesn't see anything wrong with it, but it still fails to link properly.
And yes, I HAVE tried googling this, to no avail.
Changing:
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
to:
target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
worked flawlessly

Linking MKL in catkin_make / Cmake

I have a large codebase with multiple files and multiple Cmake files as well. The structure is src/folder1 src/folder2 etc and I run catkin_make -j1 from the src directory level. I am unable to link MKL. A small clip of the error is
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_sparse_d_xESB_SpMV_8_i4'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_spblas_zcoo0ssunc__mmout_par'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_spblas_zcoo1ntuuf__mmout_par'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_spblas_lp64_scsr0ntunc__smout_par'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_spblas_lp64_ccsr0nd_nc__svout_seq'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_spblas_ccoo1stlnf__svout_seq'
/usr/bin/ld: /opt/intel/oneapi/mkl/2021.3.0/lib/intel64/libmkl_intel_thread.so: undefined reference to `mkl_pds_pds_her_pos_fwd_ker_seq_nrhs_cmplx'
collect2: error: ld returned 1 exit status
I even added the following line to each cmake:
SET(GCC_COVERAGE_LINK_FLAGS " -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl")
The code does not use MKL BLAS directly but rather it is used by libraries like Opencv and Eigen (OpenCV was built with MKL).
I do define EIGEN_USE_MKL_ALL.
How do I link MKL?
You can make use of oneMKL link line advisor which recommends what are the required libraries and necessary compiler options for the use case on which you are working.
Here is the link
https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html

How to recompile ntl and gmp libraries with "-fPIC" flag & make HELib as a shared library & undefined reference to func(std::ostream&) error

a C++ code built into a shared library for HELib is using NTL and GMP static library. But it gets following error:
/usr/bin/ld: /usr/local/lib/libntl.a(FFT.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libntl.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
It was suggested int the post Click here
to recompile NTL and GMP with "-fPIC " flags.
I'am not able to find how I should do that.
Edit:
I'm able to build the shared library now after :
recompiling GMP and NTL by:
./configure --enable-shared
for gmp and
./configure SHARED=on
After make Install of HELib. I get error when I run the example codes.
The link to the Makefile : click to see makefile
Error:
g++ -g -O2 -std=c++11 -pthread -DFHE_THREADS -DFHE_BOOT_THREADS -DFHE_DCRT_THREADS -o Test_General_x Test_General.cpp -L/usr/local/lib -lntl -lgmp -lm -lfhe
/usr/local/lib/libfhe.so: undefined reference to write_raw_int(std::ostream&, long, long)'
/usr/local/lib/libfhe.so: undefined reference toread_raw_ZZ(std::istream&, NTL::ZZ&)'
/usr/local/lib/libfhe.so: undefined reference to void write_raw_vector<long>(std::ostream&, std::vector<long, std::allocator<long> > const&)'
/usr/local/lib/libfhe.so: undefined reference towriteEyeCatcher(std::ostream&, char const*)'
/usr/local/lib/libfhe.so: undefined reference to write_raw_xdouble(std::ostream&, NTL::xdouble)'
/usr/local/lib/libfhe.so: undefined reference toread_ntl_vec_long(std::istream&, NTL::Vec&)'
/usr/local/lib/libfhe.so: undefined reference to void read_raw_vector<long>(std::istream&, std::vector<long, std::allocator<long> >&)'
/usr/local/lib/libfhe.so: undefined reference toreadEyeCatcher(std::istream&, char const*)'
/usr/local/lib/libfhe.so: undefined reference to read_raw_int(std::istream&, long)'
/usr/local/lib/libfhe.so: undefined reference toread_raw_xdouble(std::istream&)'
/usr/local/lib/libfhe.so: undefined reference to write_raw_ZZ(std::ostream&, NTL::ZZ const&)'
/usr/local/lib/libfhe.so: undefined reference towrite_ntl_vec_long(std::ostream&, NTL::Vec const&, long)'
collect2: error: ld returned 1 exit status
Makefile:179: recipe for target 'Test_General_x' failed
make: *** [Test_General_x] Error 1
For NTL v11.5.1 atleast, doing ./configure --help | grep -i pic (as suggested in one of the comments for GMP) did not help at all - it found no matches. However, making a one line change in the file ntl-11.5.1/src/DoConfig (on line 17) from:
'CXXFLAGS' => '-g -O2'
to:
'CXXFLAGS' => '-g -O2 -fPIC',
solved the problem for me.
Marc Glisse provided the answer for the first two parts of the question.For the third part "Undefined Reference error" the answer is I'd not compiled and linked a x.cpp containing the functions that caused the undefined reference into my shared library. hence check : nm -CD /usr/local/lib/libfhe.so to see if these functions are listed with a linking address or not. If not then check which code provides this functionality. Link that code to the shared library.

CMake linker not working correctly

I am trying to build my project using CMake but I am having error linking required libraries. I have this CMakeLists.txt in the root folder of my project:
cmake_minimum_required(VERSION 2.6)
project(test)
add_subdirectory(src)
And in my src folder, alongside my source files I have this CMakeLists.txt:
set (CMAKE_CXX_FLAGS "Wall -std=c++11" )
set (CMAKE_EXE_LINKER_FLAGS "-lSDL2 -lGL" )
file (GLOB SRCS *.cpp *.h )
add_executable(engine ${SRCS} )
I then go into the build folder and do cmake .. and it runs without any errors. When I do make, the compilation runs without any errors as well, but when it gets to the linking part, I get these errors:
CMakeFiles/test.dir/Application.cpp.o: In function `Application::onExecute()':
Application.cpp:(.text+0x41): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0x4e): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0xd7): undefined reference to `SDL_PollEvent'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::render()':
Application.cpp:(.text+0x17b): undefined reference to `glClearColor'
Application.cpp:(.text+0x185): undefined reference to `glClear'
Application.cpp:(.text+0x194): undefined reference to `SDL_GL_SwapWindow'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::cleanUp()':
Application.cpp:(.text+0x1b2): undefined reference to `SDL_GL_DeleteContext'
Application.cpp:(.text+0x1c1): undefined reference to `SDL_DestroyWindow'
Application.cpp:(.text+0x1c6): undefined reference to `SDL_Quit'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::initialize()':
Application.cpp:(.text+0x1de): undefined reference to `SDL_Init'
Application.cpp:(.text+0x1ea): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x22b): undefined reference to `SDL_CreateWindow'
Application.cpp:(.text+0x243): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x25a): undefined reference to `SDL_Quit'
Application.cpp:(.text+0x270): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x27f): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x28e): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x29d): undefined reference to `SDL_GL_CreateContext'
Application.cpp:(.text+0x2af): undefined reference to `glGetString'
collect2: error: ld returned 1 exit status
make[2]: *** [src/test] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
I have the correct includes in my header files and I was able to compile and run using only make so I think the linker flags I tell CMake aren't being passed to the compiler. How can I fix this?
You have to use the cmake command target_link_libraries to reference SDL libs.