CMake can't find Boost_LIBRARIES variable - c++

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

Related

Undefined reference in libfreenect c++ wrapper

I want to print the number of connected devices with libfreenect in c++. As described in https://openkinect.org/wiki/C%2B%2B_Wrapper
i include the libfreenect.hpp header file in my TestKinectConnection.cpp.
My TestKinectConnection.cpp:
#include <iostream>
#include "libfreenect.hpp"
using namespace std;
int main(void) {
Freenect::Freenect nect;
freenect_context *f_ctx;
cout << nect.deviceCount() << endl;
return(0);
}
When i build with cmake --build build -- -j3 the terminal shows
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x40): undefined reference to `freenect_init'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x90): undefined reference to `freenect_select_subdevices'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0xb0): undefined reference to `pthread_create'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::~Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xa5): undefined reference to `pthread_join'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xb4): undefined reference to `freenect_shutdown'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::deviceCount()':
TestKinectConnection.cpp:(.text._ZN8Freenect8Freenect11deviceCountEv[_ZN8Freenect8Freenect11deviceCountEv]+0x17): undefined reference to `freenect_num_devices'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::operator()()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectclEv[_ZN8Freenect8FreenectclEv]+0x4f): undefined reference to `freenect_process_events_timeout'
collect2: error: ld returned 1 exit status
CMakeFiles/projektinf.dir/build.make:95: recipe for target '../bin/projektinf' failed
make[2]: *** [../bin/projektinf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/projektinf.dir/all' failed
make[1]: *** [CMakeFiles/projektinf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My CMakeLists.txt:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.10)
# Project's name
project(projektinf)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR})
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_SOURCE_DIR}/lib)
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}/src/include")
add_executable(projektinf ${PROJECT_SOURCE_DIR}/src/main/TestKinectConnection.cpp)
add_library(libfreenect ${PROJECT_SOURCE_DIR}/src/include/libfreenect.hpp)
target_link_libraries(projektinf PUBLIC libfreenect)
set_target_properties(libfreenect PROPERTIES LINKER_LANGUAGE CXX)
https://github.com/OpenKinect/libfreenect/blob/master/wrappers/cpp/libfreenect.hpp
https://github.com/OpenKinect/libfreenect/blob/master/include/libfreenect.h
In short summary, libfreenect.hpp tries to include libfreenect.h und completely fails.
From your question it is unclear whether you installed libfreenect globally or simply bundled the library with your code.
If you installed it globally, you forgot to tell CMake that the libfreenect target needs to link with libfreenect.so using the -lfreenect linker flag.
Adding the following should fix that:
set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)
The proper approach is to make libfreenect an IMPORTED target, as documented in "It's time to do CMake right".
If you bundled the .cpp with your code, you need to add the .cpp file to the add_library statement that defines the libfreenect target.

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})

Linker error while building from source code

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")

Error linking to Boost filesystem using cmake on cygwin

I'm using cmake 2.8.9, g++ 3.4.4, and Boost 1.50. in Cygwin on Windows 8 64 bit.
Here is the error message I get.
Linking CXX executable RayTracer.exe
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x89c):
undefined reference to boost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8a6):
undefined reference toboost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8b0):
undefined reference to boost::system::system_category()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o: bad reloc address 0xb in
section
.text$_ZN5boost6system14error_categoryD1Ev[boost::system::error_category::~error_category()]'
collect2: ld returned 1 exit status
CMakeFiles/RayTracer.dir/build.make:94: recipe for target
RayTracer.exe' failed make[2]: *** [RayTracer.exe] Error 1
CMakeFiles/Makefile2:64: recipe for target
CMakeFiles/RayTracer.dir/all' failed make[1]: *
[CMakeFiles/RayTracer.dir/all] Error 2 Makefile:75: recipe for target
`all' failed make: * [all] Error 2
From what I've seen, the usual problem is failing to link the boost system library, but I made sure to do that. Here is the relevant portion of my CMakeLists.txt file:
#Edit: cmake can't find the static libraries on cygwin, so I'm setting this to false for now.
SET(Boost_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(Boost 1.50 REQUIRED date_time program_options thread filesystem system unit_test_framework)
IF(${Boost_FOUND})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ENDIF()
add_executable(RayTracer
Ray_Tracer.cpp
)
target_link_libraries(RayTracer ${Boost_PROGRAM_OPTIONS_LIBRARIES})
And here's the line in my .cpp file that triggers the error:
#include <boost/filesystem.hpp>
Any idea what I'm doing wrong?
You need to tell the linker to link Boost.Filesystem and Boost.System libraries.
You can do:
target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:
target_link_libraries(RayTracer ${Boost_LIBRARIES})
For further details on the FindBoost CMake module, see the docs or run:
cmake --help-module FindBoost