I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})
Related
Find_package command is a nightmare to me.
I am trying to include some specified components in boost into my project.
Some components could not be found with find_package command for different error.
Can anyone help to explain the error reported?
case 1:
cmake_minimum_required(VERSION 3.15)
project(tryBoost)
set(CMAKE_CXX_STANDARD 14)
set(BOOST_ROOT "D:\\cygwin64\\home\\yubo\\boost_1_62_0") # either set it here or from the command line
find_package(Boost 1.62.0 REQUIRED COMPONENTS json) # header only libraries must not be added here
add_executable(tryBoost main.cpp)
I try to find json, but error reported:
No header defined for json; skipping header check
case 2:
cmake_minimum_required(VERSION 3.15)
project(tryBoost)
set(CMAKE_CXX_STANDARD 14)
set(BOOST_ROOT "D:\\cygwin64\\home\\yubo\\boost_1_62_0") # either set it here or from the command line
find_package(Boost 1.62.0 REQUIRED COMPONENTS system) # header only libraries must not be added here
add_executable(tryBoost main.cpp)
I try to find system, but error reported:
Could NOT find Boost (missing: Boost_INCLUDE_DIR system)
How boost organizes its components in sub dirs? How find_package command works when scaning boost root dir? why "header only libraries must not be added here".
thanks.
Please use below CMakeList.txt for reference.
cmake_minimum_required(VERSION 3.15)
project(tryBoost)
set(CMAKE_CXX_STANDARD 14)
#set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(BOOST_ROOT "/home/yubo/boost_1_78_0")
add_executable(tryBoost main.cpp)
set(Boost_NO_BOOST_CMAKE FALSE CACHE BOOL "" FORCE)
find_package(Boost 1.78.0 REQUIRED COMPONENTS json)
message("${Boost_FOUND}")
message("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
target_include_directories(tryBoost PUBLIC ${Boost_INCLUDE_DIRS})
message("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
message("Boost_LIBRARIES: ${Boost_LIBRARIES}")
target_link_libraries(tryBoost ${Boost_LIBRARIES})
I'm attempting to alter my CmakeLists.txt file for my currently working MacOSX version of my OpenGL engine, so that it will compile and run on windows. I'm positive that I'm doing something wrong in the way I'm writing my Cmakelists.txt and would like some help correcting it.
Have already tried a multitude of different tutorials and examples found online. The problem most likely lies in how I'm trying to link dll files but I'm unsure of what I'm doing wrong.
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")
project (sake)
add_subdirectory(src/imgui)
add_subdirectory(libs/assimp)
add_subdirectory(src/GLM/glm)
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES src/*.cpp)
add_executable(sake ${SRC_FILES})
target_link_libraries(sake imgui)
target_link_libraries(sake assimp)
# glfw
if (WIN32)
add_subdirectory(F:/glfw-3.3 F:/glfw-3.3/src)
include_directories(F:/glfw-3.3/include)
link_libraries(${GLFW_LIBRARY_DIRS})
endif (WIN32)
if(APPLE)
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
link_libraries(${GLFW_LIBRARY_DIRS})
endif (APPLE)
# opengl
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
# glew
if(WIN32)
include_directories(F:/GLEW/glew-2.1.0/include)
endif (WIN32)
if(APPLE)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
endif (APPLE)
if(WIN32)
include_directories(libs/assimp/include)
endif(WIN32)
if(APPLE)
find_package(assimp REQUIRED)
include_directories(${ASSIMP_INCLUDE_DIR})
endif(APPLE)
if (APPLE)
find_library(COCOA_LIBRARY Cocoa)
find_library(OpenGL_LIBRARY OpenGL)
find_library(IOKIT_LIBRARY IOKit)
find_library(COREVIDEO_LIBRARY CoreVideo)
SET(EXTRA_LIBS ${COCOA_LIBRARY} ${OpenGL_LIBRARY} ${IOKIT_LIBRARY} ${COREVIDEO_LIBRARY})
endif (APPLE)
target_link_libraries(
sake
glfw
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${EXTRA_LIBS}
)
add_custom_target(run
COMMAND ./sake
DEPENDS sake
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)
current error message happens when I cmake .
CMake Error at F:/glfw-3.3/src/CMakeLists.txt:95 (add_library):
add_library cannot create target "glfw" because another target with the
same name already exists. The existing target is a shared library created
in source directory "F:/glfw-3.3/src". See documentation for policy
CMP0002 for more details.
My project structure looks like this:
CMakeLists.txt
deps
glew
glfw
include
src
...
graphics
...
CMakeLists.txt
Two CMakeLists.txt files that deserve attention:
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(noam_engine)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(NE_LIBRARIES common math graphics)
FOREACH(lib ${NE_LIBRARIES})
add_subdirectory(src/${lib})
ENDFOREACH(lib)
add_executable(noam_engine src/main.cpp)
if(OPENGL_FOUND AND GLEW_FOUND)
target_include_directories(noam_engine PUBLIC include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
target_link_libraries(noam_engine ${NE_LIBRARIES})
endif()
src/graphics/CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
file(GLOB SRC "*.cpp")
add_library(graphics ${SRC})
if(OPENGL_FOUND AND GLEW_FOUND)
target_include_directories(graphics PUBLIC ${PROJECT_SOURCE_DIR}/include ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
target_link_libraries(graphics ${OPENGL_gl_LIBRARY} ${GLFW3_LIBRARY} ${GLEW_LIBRARIES})
message(STATUS "GLFW and GLEW successfully linked")
message(STATUS ${OPENGL_gl_LIBRARY})
message(STATUS ${GLFW3_LIBRARY})
message(STATUS ${GLEW_LIBRARIES})
else()
message(STATUS "Cannot find GL libraries")
endif()
In a few words I want to create a bunch of static libraries of the engine, in particular, link graphics library with GL's, and finally link all of them with the executable in a root CMakeLists.txt.
But I noticed that ${GLFW3_LIBRARY} is empty and I got a linker error, for example, when I call glfwInit(). I followed the guide during building and installation of GLFW
cmake .
make
make install
I believe headers and libraries are in /usr/local/*, but apparently CMake cannot find them or maybe I did something wrong.
The only hypothesis I have is that find_package doesn't know about glfw3Config.cmake which is in deps/glfw/*
I took the script from https://github.com/JoeyDeVries/LearnOpenGL/blob/master/cmake/modules/FindGLFW3.cmake and put it into cmake folder. Then in CMakeLists.txt I added
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
Now everything works properly
Thanks #Tsyvarev
CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I need to get a single DLL in a result of building that no need any other libraries to work. But can't link Boost libraries statically. I bootstrapped and built Boost with corresponding MinGW.
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT "..\\lib\\boost_1_59_0")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--kill-at -static-libgcc -static-libstdc++")
add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}\\..\\..\\output")
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Build output:
O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o): In function error':
O:\SampleProject\Cpp\lib\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference toboost::system::system_category()'
What else should I do to link with boost?
UPDATE: there is a list of built libraries
libboost_filesystem-mgw48-1_59.a
libboost_filesystem-mgw48-d-1_59.a
libboost_filesystem-mgw48-mt-1_59.a
libboost_filesystem-mgw48-mt-d-1_59.a
libboost_filesystem-mgw48-mt-s-1_59.a
libboost_filesystem-mgw48-mt-sd-1_59.a
libboost_filesystem-mgw48-s-1_59.a
libboost_filesystem-mgw48-sd-1_59.a
libboost_system-mgw48-1_59.a
libboost_system-mgw48-d-1_59.a
libboost_system-mgw48-mt-1_59.a
libboost_system-mgw48-mt-d-1_59.a
libboost_system-mgw48-mt-s-1_59.a
libboost_system-mgw48-mt-sd-1_59.a
libboost_system-mgw48-s-1_59.a
libboost_system-mgw48-sd-1_59.a
This looks like a linker error suggesting that you are not linking to Boost::system
You need to add system to BOOST_COMPONENTS_NEEDED. Change this line and see if it helps
set(BOOST_COMPONENTS_NEEDED system filesystem )
I'm trying to build python bindings for a library that i wrote, and i'm having some trouble getting cmake to understand that it should use the boost-python library for python 3.
Here is my cmake file:
cmake_minimum_required(VERSION 2.8)
FIND_PACKAGE(Boost COMPONENTS
system
thread
python REQUIRED)
find_package(PythonLibs REQUIRED)
INCLUDE_DIRECTORIES(${PYTHON_LIBRARIES})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
ADD_LIBRARY(
pschulze SHARED
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
python.cpp)
TARGET_LINK_LIBRARIES(pschulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_EXECUTABLE(
schulze
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
src/json-spirit/json_spirit_reader.cpp
src/json-spirit/json_spirit_value.cpp
main.cpp)
TARGET_LINK_LIBRARIES(schulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_DEFINITIONS(-std=gnu++0x -Os)
add_subdirectory (tests)
set(CMAKE_BUILD_TYPE Debug)
And this is the linker error that I get:
Linking CXX executable schulze
CMakeFiles/schulze.dir/src/schulze.cpp.o: In function `arg_to_python':
/usr/include/boost/python/converter/builtin_converters.hpp:122: undefined reference to `PyInt_FromLong'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libboost_python.so: undefined reference to `PyString_Size'
This might do the trick :
set(Python_ADDITIONAL_VERSIONS 3.2)
find_package(Boost COMPONENTS system thread python-py32 REQUIRED)