Linker error while building from source code - c++

I am trying to build an application from source. I am able to configure it using 'cmake .'. However, when I run 'make' it gives me this:
Linking CXX executable ../../bin/lux64/Release/od_batch_launcher
../../bin/lux64/Release/libBasic.so: undefined reference to `dlopen'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlclose'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlerror'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlsym'
../../bin/lux64/Release/libBasic.so: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/lux64/Release/od_batch_launcher] Error 1
make[1]: *** [src/Basic/CMakeFiles/od_batch_launcher.dir/all] Error 2
make: *** [all] Error 2
I understand that its is unable to dynamically link to a c++ library. I don't quite know how to make the necessary changes to cmake. I am running gcc version: 4.9.2 on Linux Mint 17. I would be grateful for any help. Thank you!

Try passing -DCMAKE_EXE_LINKER_FLAGS=-ldl to the CMake executable. To change the CMake build scripts, add something like:
target_link_libraries(target_name dl)
where target_name is basically the executable name without any extensions (e.g. .exe).
EDIT: Actually, I just reread you question, and I'm putting this in the wrong place. You really want:
target_link_libraries(Basic dl)
Apparently, there were also pthread-related errors, so you'd also have to add:
target_compile_options(Basic PUBLIC -pthread)
Both of these go in whichever file contains add_library(Basic) (usually CMakeLists.txt).
EDIT 2: Instead of target_compile_options, try:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

Related

CMake can't find Boost_LIBRARIES variable

Final Edit: Solved.
Guys the problem occured because of the "WinSock32".
I've added
target_link_libraries(modernC__ -lws2_32)
and the code has been builded.
I have been using the Boost library on CLion in Ubuntu for about 1 year. But this week I also decided to install it on the Windows operating system.
so I downloaded the Boost library on GitHub and installed the Boost library using Find.Boost first and then "Find.Boost".
After writing the necessary commands in CMake settings section in CLion, I noticed that the variable $ {Boost_LIBRARIES} is empty.
When I don't use the message () function, the "CMake" part of the project does not give an error, but I get the error "undefined reference" after "build". Below are the CMake commands I wrote on CLion and the errors I received.
cmake_minimum_required(VERSION 3.16)
project(modernC__)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.66.0)
message(${Boost_INCLUDE_DIR})
message(${Boost_FOUND})
message(${Boost_LIBRARY_DIRS})
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
add_executable(modernC__
main.cpp
#concurrencyExampleOne.cpp
#adapterExampleOne.cpp
)
target_link_libraries(modernC__ ${Boost_LIBRARIES})
message(${Boost_LIBRARIES})
endif()
My output is:
"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Berke\CLionProjects\modernC++
C:/boost/include/boost-1_66
TRUE
C:/boost/lib
CMake Error at CMakeLists.txt:22 (message):
message called with incorrect number of arguments
-- Configuring incomplete, errors occurred!
In this case, obviously Cmake is currently unable to find the .lib files that should link to my code.
My question is; How can I permanently drop .lib files into this variable, or is there any other way to do this?
If I do not use "message(${Boost_LIBRARIES}" so the compiler gives me this error;
[ 50%] Linking CXX executable modernC__.exe
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:676: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:679: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:709: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:721: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv':
C:/boost/include/boost-1_66/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `_imp__WSAStartup#8'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `_imp__WSACleanup#0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\modernC__.dir\build.make:86: modernC__.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/modernC__.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/modernC__.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: modernC__] Error 2
I think, you should specify boost libraries (as components) explicitly.
And you should add libraries to target.
Like this:
find_package(Boost COMPONENTS filesystem system locale context REQUIRED)
...
target_link_libraries(${PROJECT_NAME}
...
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_LOCALE_LIBRARY}
${Boost_CONTEXT_LIBRARY}
...
)

CMake undefined reference to `mg_printf'

I've been trying to work this out for a while and I've decided to ask for help.
I'm trying to include mongoose in my project, so in my api_and_json.cpp file I have:
extern "C"{
#include "mongoose/mongoose.h"
}
My cmake file currently looks like this:
cmake_minimum_required(VERSION 2.8)
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
set(CTEST_BUILD_FLAGS -j${N})
set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
endif()
project(api_and_json)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
include_directories("${PROJECT_SOURCE_DIR}")
add_executable(api_and_json ${PROJECT_SOURCE_DIR}/api_and_json.cpp)
I've tried a few permutations, so I've stripped it back to how it was before.
CMake works fine, but when I try make I get:
...
[ 50%] Linking CXX executable ../bin/api_and_json
CMakeFiles/api_and_json.dir/api_and_json.cpp.o: In function `ev_handler(mg_connection*, int, void*)':
api_and_json.cpp:(.text+0xa1): undefined reference to `mg_printf'
api_and_json.cpp:(.text+0xb7): undefined reference to `mg_printf_http_chunk'
api_and_json.cpp:(.text+0xcd): undefined reference to `mg_send_http_chunk'
CMakeFiles/api_and_json.dir/api_and_json.cpp.o: In function `main':
api_and_json.cpp:(.text+0x10c): undefined reference to `mg_mgr_init'
api_and_json.cpp:(.text+0x184): undefined reference to `mg_bind_opt'
api_and_json.cpp:(.text+0x1dc): undefined reference to `mg_set_protocol_http_websocket'
api_and_json.cpp:(.text+0x32b): undefined reference to `mg_mgr_poll'
collect2: error: ld returned 1 exit status
CMakeFiles/api_and_json.dir/build.make:94: recipe for target '../bin/api_and_json' failed
make[2]: *** [../bin/api_and_json] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/api_and_json.dir/all' failed
make[1]: *** [CMakeFiles/api_and_json.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I'm sure I'm missing something simple, any help will be appreciated.
I've looked elsewhere including:
What is an undefined reference/unresolved external symbol error and how do I fix it?
This reference did not help me, but the suggestion by Rama did.
You forgot to link against mongoose.
Add this line at the end of your CmakeList.txt:
target_link_libraries (api_and_json mongoose)

Can't compile with CMake and ncurses

I'm aware similar questions have been posted, but I can't see how they relate to my case. I will preface this question by saying I'm not very familiar with CMake, so it's entirely possible this is a quick fix and I just don't see it!
I am collaborating on a project, and yesterday my teammate added the ncurses library to the project to build a terminal GUI. Ever since ncurses was added, I haven't been able to compile my project. However, I did install all 6 ncurses-* packages, so it should behave with my machine.
I have pulled down the latest version of the master branch from our GitHub repo, which compiles and runs perfectly on his machine. However it won't even compile on mine.
System:
Linux Mint 18.1 Cinnamon
CMake Version 3.5.1
Things I've tried:
I deleted CMakeCache.txt and reloaded it
I deleted my entire CMake build directory and redid make
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(irc)
set(SHARED_FLAGS " -Wall -Wextra -Wshadow -Werror -g -D_POSIX_C_SOURCE=200809L -lncurses")
set(CMAKE_C_FLAGS "-std=c11 ${SHARED_FLAGS}")
set(CMAKE_CXX_FLAGS "-std=c++11 ${SHARED_FLAGS}")
include_directories(include)
add_library(client_core SHARED src/client/irc_client.c)
add_executable(client src/client/irc_client_gui.c)
add_executable(server src/server/irc_server.c)
// SOLUTION - MISSING LINE
target_link_libraries(client ncurses)
install(FILES include/irc_client.h DESTINATION include)
install(FILES include/irc_server.h DESTINATION include)
CMakeOutput:
CMakeFiles/client.dir/src/client/irc_client_gui.c.o: In function `main':
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:17:
undefined reference to `initscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:19:
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21:
undefined reference to `wgetnstr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22:
undefined reference to `LINES'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22:
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23:
undefined reference to `wgetch'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:24:
undefined reference to `endwin'
collect2: error: ld returned 1 exit status
CMakeFiles/client.dir/build.make:94: recipe for target 'client' failed
make[2]: *** [client] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/client.dir/all'
failed
make[1]: *** [CMakeFiles/client.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
Interestingly enough, if I copy/paste irc_client_gui.c somewhere else on my machine, compile it manually with gcc and run it, it works perfectly. So the issue isn't my machine, it's something with CMake trying to compile my entire project. Any ideas? Thanks so much!
The solution is to add a line like this:
target_link_libraries(client ncurses)
This tells CMake that when it is linking the client target, it should use the -lncurses option to link in the ncurses library.

Undefined Reference error using OpenGL in CLion

So, I am using freeglut to try to do some openGL stuff, but I keep getting errors saying that references are undefined:
"C:\Program Files (x86)\JetBrains\CLion 2016.2.1\bin\cmake\bin\cmake.exe" --build C:\Users\Nick\.CLion2016.2\system\cmake\generated\cs455opengl-9b23e7f0\9b23e7f0\Debug --target all -- -j 4
Scanning dependencies of target cs455openGL
[ 50%] Building CXX object CMakeFiles/cs455openGL.dir/main.cpp.obj
[100%] Linking CXX executable cs455openGL.exe
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z4initv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:12: undefined reference to `glClearColor#16'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:13: undefined reference to `glMatrixMode#4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:14: undefined reference to `glLoadIdentity#0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:15: undefined reference to `glOrtho#48'
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z7displayv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:20: undefined reference to `glClear#4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:21: undefined reference to `glColor3f#12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:22: undefined reference to `glVertex3f#12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:23: undefined reference to `glVertex3f#12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:24: undefined reference to `glVertex3f#12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:25: undefined reference to `glVertex3f#12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:26: undefined reference to `glEnd#0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:27: undefined reference to `glFlush#0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cs455openGL.dir\build.make:95: recipe for target 'cs455openGL.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cs455openGL.dir/all' failed
mingw32-make.exe[2]: *** [cs455openGL.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/cs455openGL.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
I am using MinGW with CLion to do this project. I thought that I got everything correctly. I moved the appropriate files into the include folder in MinGW, as well as the bin folder, and also the lib folder. Then, I have this in my CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(cs455openGL)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES main.cpp)
add_executable(cs455openGL ${SOURCE_FILES})
link_directories(${OPENGL_gl_LIBRARY})
target_link_libraries(cs455openGL libfreeglut.a libfreeglut_static.a)
The libraries I linked were the only library files that freeglut came with. I've been scouring the internet for answers and no one seems to have run into this.
So, what am I missing? CLion doesn't show any errors before it is compiled. I can even go into the functions in the header files provided by freeglut. Why then, are these functions not defined in my program?
This is a linking error, and tells you that the linker does not find the functions defined by the OpenGL library.
You have to add ${OPENGL_LIBRARIES} to target_link_libraries.
And for glut - and any other library - you should not use the library name (libfreeglut.a) directly, but always use the variables populated by the find_package:
target_link_libraries(cs455openGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

C++ CMake undefined reference when linking an executable to a third party dependant shared library

I read a lot of related topics (like 1, 2, 3) but did not find the answer by myself so here I am.
I have a CMake project that builds and executable, let say "x". I created a shared library named "a.so" that depends on other shared library called "b.so". I want to use "a" in "x".
Here is my simplified "x" CMakelists.txt:
SET(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
LINK_DIRECTORIES(${ROOT_DIR}/lib/a/bin/) # contains liba.so
INCLUDE_DIRECTORIES(${ROOT_DIR}/lib/a/include/) # contains "a" headers
ADD_EXECUTABLE(x ${SOURCE})
TARGET_LINK_LIBRARIES(x a)
"x" compilation output extract:
Linking CXX executable ../bin/x
/usr/bin/cmake -E cmake_link_script CMakeFiles/x.dir/link.txt --verbose=1
/usr/lib64/ccache/c++ -std=c++0x CMakeFiles/x.dir/src/main /Main.cpp.o
... -L/.../lib/a/bin -rdynamic -la -Wl,-rpath,/.../lib/a/bin
"a" and "b" do compile.
The problem is when I want to compile x I get errors when linking: undefined reference to 'function name'.
I tried to link against "b" too but it's still not working.
Here "b" also appears but I get the same error...
Linking CXX executable ../bin/x
/usr/bin/cmake -E cmake_link_script CMakeFiles/x.dir/link.txt --verbose=1
/usr/lib64/ccache/c++ -std=c++0x CMakeFiles/x.dir/src/main
/Main.cpp.o -o ../bin/x -L/.../lib/b/bin -L/.../lib/a/bin
-rdynamic -lb -la -Wl,-rpath,/.../lib/b/bin:/.../lib/a/bin
Here is the error output:
$ make
[ 20%] Automatic moc for target x
Linking CXX executable ../bin/x
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params_set_start_threshold'
/.../lib/b/bin/b.so: undefined reference to `snd_seq_delete_simple_port'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_info_set_device'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params_set_silence_threshold'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_hw_params_any'
/.../lib/b/bin/b.so: undefined reference to `snd_seq_drain_output'
/.../lib/b/bin/b.so: undefined reference to `snd_ctl_pcm_next_device'
...
collect2: error: ld returned 1 exit status
CMakeFiles/x.dir/build.make:163: recipe for target '../bin/x' failed
make[2]: *** [../bin/x] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/x.dir/all' failed
make[1]: *** [CMakeFiles/x.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
I don't see what's wrong and I'm now confused as I tried many things to get the thing working... but nothing worked.
Any idea?
Thanks!
I asked you for the error output in the comments above, because it can help to search the undefined symbols/references.
So, it seems that your b.so cannot find the ALSA library. If you use CMake 3.0 (and up) you can add it to your CMakeLists.txt via
find_package( ALSA )
and can append ${ALSA_INCLUDE_DIR} to your include path and ${ALSA_LIBRARY} to your linker path.
"b" was missing a specific dependant library. I added it via the FIND_PACKAGE CMake directive in the "b" CMakeLists.txt.
It solved the problem.
The above "x" CMakeLists.txt is thus correct.