I have the filesystem in my cmake:
find_package(Boost 1.63.0 COMPONENTS program_options system filesystem thread REQUIRED)
When I compile it says Found the following Boost libraries and filesystem is included.
Everything compiles fine. However once I try to include:
#include <boost/filesystem.hpp>
I get the following error. What's happening here?
CMakeFiles\runBasicBrimusTests.dir/objects.a(boost_filesystem_check.cpp.obj):
In function `_static_initialization_and_destruction_0':
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:221:
undefined reference to `boost::system::generic_category()'
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:222:
undefined reference to `boost::system::generic_category()'
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:223:
undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
Brimus-Test\basic_tests\CMakeFiles\runBasicBrimusTests.dir\build.make:910:
recipe for target 'Brimus-Test/basic_tests/runBasicBrimusTests.exe'
failed CMakeFiles\Makefile2:293: recipe for target
'Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/all'
failed mingw32-make.exe[3]: ***
[Brimus-Test/basic_tests/runBasicBrimusTests.exe] Error 1
mingw32-make.exe[2]: ***
[Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/all] Error
2 mingw32-make.exe[1]: ***
[Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/rule]
Error 2 CMakeFiles\Makefile2:305: recipe for target
'Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/rule'
failed Makefile:169: recipe for target 'runBasicBrimusTests' failed
mingw32-make.exe: *** [runBasicBrimusTests] Error 2
You should add target link libraries in cmake.
find_package(Boost 1.63.0 COMPONENTS program_options system filesystem thread
REQUIRED)
target_link_libraries(mytarget
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
Related
I have compiled ANN-library and need to use it in a C++ file for segmentation evaluation
I have set the CMakeList.txt, which is using ITK and ANN libraries as follows:
PROJECT(EvaluateSegmentationResult)
cmake_minimum_required(VERSION 3.14)
set(ANN_LIB /home/user/tools/ann_1.1.2/lib/)
set(ANN_PATH /home/user/tools/ann_1.1.2/include/)
#FIND_PACKAGE(ITK)
find_package(ITK COMPONENTS
ITKBinaryMathematicalMorphology
ITKCommon
ITKIOImageBase
ITKImageFunction
ITKImageGrid
ITKImageIntensity
ITKMathematicalMorphology
ITKThresholding
ITKImageIO
)
IF(ITK_FOUND)
INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
MESSAGE(FATAL_ERROR
"ITK not found. Please set ITK_DIR.")
ENDIF(ITK_FOUND)
FIND_PATH(ANN_PATH NAMES ANN)
FIND_LIBRARY(ANN_LIB NAMES ann PATHS ${ANN_PATH})
INCLUDE_DIRECTORIES(${ANN_PATH})
ADD_EXECUTABLE( EvaluateSegmentationResult EvaluateSegmentationResult.cpp)
#TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ITKIO ITKBasicFilters ITKCommon ${ITK_LIBRARIES} ${ANN_LIB})
TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ${ITK_LIBRARIES} ${ANN_LIB})
However, it raises an error once I am compiling the C++ file:
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:86: undefined reference to `annAllocPts(int, int)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:95: undefined reference to `ANNkd_tree::ANNkd_tree(double**, int, int, int, ANNsplitRule)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:135: undefined reference to `annAllocPts(int, int)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:144: undefined reference to `ANNkd_tree::ANNkd_tree(double**, int, int, int, ANNsplitRule)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:175: undefined reference to `annDeallocPts(double**&)'
/home/user/tools/EvaluationSourceCode/EvaluateSegmentationResult.cpp:176: undefined reference to `annDeallocPts(double**&)'
collect2: error: ld returned 1 exit status
CMakeFiles/EvaluateSegmentationResult.dir/build.make:128: recipe for target 'EvaluateSegmentationResult' failed
make[3]: *** [EvaluateSegmentationResult] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/EvaluateSegmentationResult.dir/all' failed
make[2]: *** [CMakeFiles/EvaluateSegmentationResult.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/EvaluateSegmentationResult.dir/rule' failed
make[1]: *** [CMakeFiles/EvaluateSegmentationResult.dir/rule] Error 2
Makefile:118: recipe for target 'EvaluateSegmentationResult' failed
make: *** [EvaluateSegmentationResult] Error 2
It seems the issue is related to link libraries. should I add any specific line or module to the CMakeList.txt?
Those methods are defined in ANN.lib itself, so you haven't missed adding any libraries. The ${ANN_PATH} variable passed as the PATH argument in your find_library call points to the header include folder. You should make it point to the folder that contains the library, and do an if-check to see it is found before proceeding to the target_link_libraries call.
I could solve the issue by just adding -lANN -LlibANN to the target_link_libraries() call:
TARGET_LINK_LIBRARIES( EvaluateSegmentationResult ${ITK_LIBRARIES} -lANN -LlibANN)
I have been trying to integrate boost.Log into my project, but when building I am receiving the error message:
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
I have checked the ensure I do not have BOOST_NO_EXCEPTIONS defined anywhere, but am still stumped as to why I am getting this error. My CMAKE file is as follows:
set(Boost_INCLUDE_DIR /usr/lib/cross/boost/include)
set(Boost_LIBRARY_DIR /usr/lib/cross/boost/lib)
find_package(Boost 1.63.0 COMPONENTS system filesystem log REQUIRED)
message(status "** Boost Include: ${Boost_INCLUDE_DIR}")
message(status "** Boost Libraries: ${Boost_LIBRARY_DIRS}")
message(status "** Boost Libraries: ${Boost_LIBRARIES}")
set (PROJECT_LINK_LIBS thrift crypto ssl sqlcipher)
link_directories( $ENV{LD_LIBRARY_PATH}
${Boost_LIBRARY_DIRS}
)
include_directories( $ENV{LD_INCLUDE_PATH}
$ENV{LD_INCLUDE_PATH}/sqlcipher
${Boost_INCLUDE_DIR}
)
add_executable(databaseManager ${MY_SRC})
target_link_libraries(databaseManager ${Boost_LIBRARIES} ${PROJECT_LINK_LIBS} )
The output from the FIND for the libraries seem to be ok:
-- Boost version: 1.63.0
1> -- Found the following Boost libraries:
1> -- system
1> -- filesystem
1> -- log
1> -- date_time
1> -- log_setup
1> -- thread
1> -- regex
1> -- chrono
1> -- atomic
1> status** Boost Include: /usr/lib/cross/boost/include
1> status** Boost Libraries: /usr/lib/cross/boost/lib
1> status** Boost Libraries:
/usr/lib/cross/boost/lib/libboost_system.so;/usr/lib/cross/boost/lib/libboost_filesystem.so;/usr/lib/cross/boost/lib/libboost_log.so;/usr/lib/cross/boost/lib/libboost_date_time.so;/usr/lib/cross/boost/lib/libboost_log_setup.so;/usr/lib/cross/boost/lib/libboost_thread.so;/usr/lib/cross/boost/lib/libboost_regex.so;/usr/lib/cross/boost/lib/libboost_chrono.so;/usr/lib/cross/boost/lib/libboost_atomic.so
Finally, the verbose output of the linking step where it fails:
/home/imx7/build/imx7-firmware-dev-package/src/maketools/obj.iMX7/bin/arm-devolo-linux-g++ -fexceptions -frtti -pthread -O3 -DNDEBUG -rdynamic CMakeFiles/databaseManager.dir/DatabaseManagerEventHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerServer.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerService.cpp.o CMakeFiles/databaseManager.dir/Logger.cpp.o CMakeFiles/databaseManager.dir/dbConnection.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_types.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/DatabaseManager.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_types.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Info.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Supervision.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_types.cpp.o -o databaseManager -L/usr/lib/cross/official_libs -L/usr/lib/cross/boost/lib -Wl,-rpath,/usr/lib/cross/official_libs:/usr/lib/cross/boost/lib: /usr/lib/cross/boost/lib/libboost_system.so /usr/lib/cross/boost/lib/libboost_filesystem.so /usr/lib/cross/boost/lib/libboost_chrono.so /usr/lib/cross/boost/lib/libboost_log.so /usr/lib/cross/boost/lib/libboost_date_time.so /usr/lib/cross/boost/lib/libboost_log_setup.so /usr/lib/cross/boost/lib/libboost_thread.so /usr/lib/cross/boost/lib/libboost_regex.so /usr/lib/cross/boost/lib/libboost_atomic.so -lthrift -lcrypto -lssl -lsqlcipher
/home/imx7/build/imx7-firmware-dev-package/src/maketools/binaries.iMX7/tools/bin/../lib/gcc/arm-devolo-linux-gnueabihf/4.9.4/../../../../arm-devolo-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /usr/lib/cross/official_libs/libsqlcipher.so, not found (try using -rpath or -rpath-link)
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/databaseManager.dir/build.make:470: recipe for target 'src/databaseManager' failed
make[2]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Release'
CMakeFiles/Makefile2:88: recipe for target 'src/CMakeFiles/databaseManager.dir/all' failed
make[1]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Release'
make[2]: *** [src/databaseManager] Error 1
make[1]: *** [src/CMakeFiles/databaseManager.dir/all] Error 2
Makefile:132: recipe for target 'all' failed
make: *** [all] Error 2
Does anyone have any idea what else I can check or do to get past this error?
Cheers!
******* EDIT *********
There have been a few suggestions about adding exception to the COMPONENTS list. I have tried and tested this and unfortunately receive the same error.
The only thing of note is all the main libraries are linked dynamically (.so versions pulled in) and boost_exception is pulled in statically (.a version).
New version of find_package command:
find_package(Boost 1.63.0 COMPONENTS system atomic filesystem log exception REQUIRED)
Build error with above change:
/home/imx7/build/imx7-firmware-dev-package/src/maketools/obj.iMX7/bin/arm-devolo-linux-g++ -fexceptions -frtti -pthread -g -rdynamic CMakeFiles/databaseManager.dir/DatabaseManagerEventHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerServer.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerService.cpp.o CMakeFiles/databaseManager.dir/Logger.cpp.o CMakeFiles/databaseManager.dir/dbConnection.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_types.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/DatabaseManager.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_types.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Info.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Supervision.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_types.cpp.o -o databaseManager -L/usr/lib/cross/official_libs -L/usr/lib/cross/boost/lib -Wl,-rpath,/usr/lib/cross/official_libs:/usr/lib/cross/boost/lib: /usr/lib/cross/boost/lib/libboost_system.so /usr/lib/cross/boost/lib/libboost_atomic.so /usr/lib/cross/boost/lib/libboost_filesystem.so /usr/lib/cross/boost/lib/libboost_log.so /usr/lib/cross/boost/lib/libboost_exception.a /usr/lib/cross/boost/lib/libboost_date_time.so /usr/lib/cross/boost/lib/libboost_log_setup.so /usr/lib/cross/boost/lib/libboost_thread.so /usr/lib/cross/boost/lib/libboost_regex.so /usr/lib/cross/boost/lib/libboost_chrono.so -lthrift -lcrypto -lssl -lsqlcipher
/home/imx7/build/imx7-firmware-dev-package/src/maketools/binaries.iMX7/tools/bin/../lib/gcc/arm-devolo-linux-gnueabihf/4.9.4/../../../../arm-devolo-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /usr/lib/cross/official_libs/libsqlcipher.so, not found (try using -rpath or -rpath-link)
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/databaseManager.dir/build.make:471: recipe for target 'src/databaseManager' failed
make[2]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Debug'
CMakeFiles/Makefile2:88: recipe for target 'src/CMakeFiles/databaseManager.dir/all' failed
make[1]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Debug'
make[2]: *** [src/databaseManager] Error 1
make[1]: *** [src/CMakeFiles/databaseManager.dir/all] Error 2
Makefile:132: recipe for target 'all' failed
make: *** [all] Error 2
An update if anyone else ever comes across this.
The only way I was able to get it to work was with having the following snippet of code:
#define BOOST_NO_EXCEPTIONS
#include <boost/throw_exception.hpp>
void boost::throw_exception(std::exception const & e){
//do nothing
}
I played with the ordering of the libraries, with including them all explicitly, with using findBoost.cmake and all to no avail. Just a random quirk I guess!
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)
I want to use boost.asio header in my project, but when I include it's .hpp file I got this output error on compile:
I need boost.asio for crow to route my web request.
/home/john/Downloads/clion-1.2.4/bin/cmake/bin/cmake --build /home/john/.CLion12/system/cmake/generated/a3f08900/a3f08900/Release --target rcp -- -j 8
[ 50%] Linking CXX executable /home/john/projects/rightChoiceProperty/bin/rcp
CMakeFiles/rcp.dir/main.cpp.o: In function _GLOBAL__sub_I_main':
main.cpp:(.text.startup+0x53): undefined reference toboost::system::generic_category()'
main.cpp:(.text.startup+0x58): undefined reference to boost::system::generic_category()'
main.cpp:(.text.startup+0x5d): undefined reference toboost::system::system_category()'
main.cpp:(.text.startup+0x62): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/rcp.dir/build.make:94: recipe for target '/home/john/projects/rightChoiceProperty/bin/rcp' failed
make[3]: * [/home/john/projects/rightChoiceProperty/bin/rcp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rcp.dir/all' failed
make[2]: [CMakeFiles/rcp.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rcp.dir/rule' failed
make[1]: [CMakeFiles/rcp.dir/rule] Error 2
Makefile:118: recipe for target 'rcp' failed
make: * [rcp] Error 2
I'm using CLion 1.2.4 as IDE
this is my main.cpp content:
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
And this is my CMakeLists.txt file content:
cmake_minimum_required(VERSION 3.3)
project(rcp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lboost_system")
set(SOURCE_FILES main.cpp)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
include_directories("libraries/boost")
add_executable(rcp ${SOURCE_FILES})
Thank you so much
The -l option is not a compiler option, it's a linker options, so you're setting it for the wrong variable as CMAKE_CXX_FLAGS are only for the compiler.
Instead use e.g. target_link_libraries to add libraries. Like
target_link_libraries(rcp boost_system)
But what you really should do is to find the system-installed Boost libraries and use those. You do that with find_package:
find_package(Boost
REQUIRED COMPONENTS asio system)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(rcp ${Boost_LIBRARIES})
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