Adding Boost.Log to boost libraries using CMake - c++

I am trying to add Boost.Log to the Boost libraries using CMake, but I am having trouble when trying to link into my program.
I've added a wrapper around the Boost.Log and generated a shared library called libcls_utils.so. The Boost libraries (along with Boost.Log) appear to be built and generate all the .so files properly in the correct location, and so does libcls_utils.so.
When I try to link my file, I get the following error:
/media/data/workspace/mdxdev/tmp/staging/i686-mv-linux/usr/lib/libcls_utils.so: undefined reference to `boost::log_mt_posix::sinks::basic_text_file_backend::construct(boost::fil‌​esystem2::basic_path, std::allocator >, boost::filesystem2::path_traits> const&, std::_Ios_Openmode, unsigned long long, boost::function0 const&, bool)
As far as I can tell, I'm linking against all the correct libraries. Has anyone tried this before successfully? What am I doing wrong?
I am using CMake 2.8.8, Boost-1.49.0 and Boost.Log from the svn trunk.

You might need to define BOOST_LOG_DYN_LINK:
g++ -DBOOST_LOG_DYN_LINK blog.cpp -lboost_log -lpthread

Related

CMake - Linker error with static yaml-cpp as git submodule

I am trying to use yaml-cpp in my project. I am using cmake to build both my project and yaml-cpp which is a git submodule in my project.
I have the boost library on my computer (required by yaml-cpp).
I am building it statically. Using add_subdirectory in my main CMakeLists.txt then using target_link_libraries(components yaml-cpp) (component is a static library I am using on different modules of my project).
I have an error during the linking phase for one of my source files :
CMakeFiles/rsa-packer.dir/options/addObject.cpp.o : In function « YAML::detail::node& YAML::detail::node_data::get<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::shared_ptr<YAML::detail::memory_holder>) » :
~/git/apps/rsa-packer/options/addObject.cpp:(.text._ZN4YAML6detail9node_data3getINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERNS0_4nodeERKT_N5boost10shared_ptrINS0_13memory_holderEEE[_ZN4YAML6detail9node_data3getINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERNS0_4nodeERKT_N5boost10shared_ptrINS0_13memory_holderEEE]+0x157) : undefined reference to: « YAML::detail::node_data::convert_to_map(boost::shared_ptr<YAML::detail::memory_holder>) »
I checked the make output, there is the -lyaml-cpp option.
I checked the libyaml-cpp.a file, it contains the function (but I do not really understand the output of nm :
000000000000005d r .L__PRETTY_FUNCTION__._ZN4YAML6detail9node_data14convert_to_mapESt10shared_ptrINS0_13memory_holderEE
0000000000000940 T _ZN4YAML6detail9node_data14convert_to_mapESt10shared_ptrINS0_13memory_holderEE
The weird thing is :
// this works
YAML::Node node = YAML::Load(manifest.getStream());
// this does not work
if(node["objects"]) {
std::cout << "no objects" << std::endl;
}
I can build the node object using the YAML::Load function that works and is found in the static lib.
But I can't call node["..."] because the function is not found in the lib.
I have no idea what is causing this error.
I don't know if it can lead to problems, but I am compiling my project in C++14 and the yaml-cpp lib is built using c++11.
Thanks!
Ok, I was tired...
mongodb had already installed yaml-cpp on my computer. I was using the headers files from mongodb with the static library I just compiled from sources... The header was calling the function with boost::shared_ptr and my library was implemented using std::shared_ptr

GDAL Not Linking

I'm trying to get my program running on Windows. It depends on GDAL, a library for loading GIS data. It compiles and links fine on both Linux and macOS. I'm using CMake with MinGW and I'm running into linking errors like this:
undefined reference to `GDALRasterBand::RasterIO(GDALRWFlag, int, int, int, int, void*, int, int, GDALDataType, long, long, GDALRasterIOExtraArg*)'
CMakeFiles\Routes.dir/objects.a(elevation.cpp.obj): In function `ZN13ElevationData9calcStatsEv':
C:/Users/Logan/Documents/Routes/src/elevation/elevation.cpp:138: undefined reference to `GDALDataset::GetRasterXSize()'
C:/Users/Logan/Documents/Routes/src/elevation/elevation.cpp:139: undefined reference to `GDALDataset::GetRasterYSize()'
CMakeFiles\Routes.dir/objects.a(elevation.cpp.obj): In function `ZN13ElevationData17createOpenCLImageEv':
C:/Users/Logan/Documents/Routes/src/elevation/elevation.cpp:206: undefined reference to `GDALRasterBand::RasterIO(GDALRWFlag, int, int, int, int, void*, int, int, GDALDataType, long, long, GDALRasterIOExtraArg*)'
I compiled GDAL with VS2017 and I've verified that it is installed where I am specifying in my CMake File. Here is the relevant portions of the CMakeLists.txt:
IF (WIN32)
message(STATUS "Compiling for Windows")
set(GDAL_LIBRARY "C:/warmerda/bld/lib/gdal_i.lib")
set(GDAL_INCLUDE_DIR "C:/warmerda/bld/include/")
find_package(OpenCL REQUIRED)
include_directories(${OpenCL_INCLUDE_DIRS})
ELSE()
...
message(STATUS ${GDAL_LIBRARY})
target_link_libraries(Routes ${GDAL_LIBRARY} ${OpenCL_LIBRARIES})
I almost never do development on Windows so I'm kind of stuck. I've tried linking against the dll as well with no avail. Any ideas?
Apparently you're attempting to link a library you compiled with VC++ with object
files that you're compiling with GCC (MinGW). That doesn't work.
GCC and VC++ have different and incompatible ABIs,
and in particular different name mangling protocols.
Hence the mangled function names emitted by MinGW in your object code will
not match any exported by your VC++ compiled library. You will need to
build the GDAL library with MinGW.
Thanks #MikeKinghan! I had been pulling my hair out for couple of days! Your answer clicked right away. For those who want to have no headache with the libraries, just use MSYS2 (msys2.org). Then using pacman in the msys2 command prompt, get the MingW (pacman -S mingw-w64-x86_64-gcc) and GDAL (pacman -S mingw-w64-x86_64-gdal).

Linking with BLAS OS X

I am currently trying to compile a program using a library that I'm not very familiar with. When I run the compiler, I get the following output/error:
Undefined symbols for architecture x86_64:
"_cblas_ddot", referenced from:
shark::LDA::train(shark::LinearClassifier<shark::blas::vector<double> >&, shark::LabeledData<shark::blas::vector<double>, unsigned int> const&) in libshark.a(LDA.cpp.o)
shark::LDA::train(shark::LinearClassifier<shark::blas::vector<double> >&, shark::WeightedLabeledData<shark::blas::vector<double>, unsigned int> const&) in libshark.a(LDA.cpp.o)
My current idea is that this is from missing BLAS library, which I just downloaded and compiled (from fortran code?).
The command-line arg for compiling looks like thsi
g++ -o example -I/to/boost/include -I/to/shark/include -L/to/boost/lib -L/to/shark/lib -L/to/BLAS -lshark -lblas -l(boost_flags)
In /to/BLAS have a libblas.a, and I ran nm libblas.a which gives a line
libblas.a(ddot.o):
00000000000001c0 s EH_frame1
0000000000000000 T _ddot_
Without being too secure abut this, I assume that this means that the library does have the symbol, but without the _cblas_ prefix.
What can I do from this point? Can I change the symbols name? Or do I need to link this library with something else?
I use OS X Mavericks
Thanks in advance : )
OS X ships with BLAS (including cblas) already installed (it's part of the Accelerate.framework). There's no need to download and build your own. Just link with -lcblas.
Solution was as suggested by Logicrat, that I needed other files, I downloaded te source code from here and build it with cmake/make very seamless. Linking with the libraries that came from this resolved the issue.

Using ArrayFire libraries

I downloaded the free version of ArrayFire library. I tried to test matrix multiplication with it. However I got a bunch of undefined references such as
matrixdata.cpp:(.text+0x19e5): undefined reference to af::array::array<unsigned char>(int, int, unsigned char const*, af::af_source_t, int)
matrixdata.cpp:(.text+0x1a22): undefined reference to af::array::operator=(af::array const&)'
matrixdata.cpp:(.text+0x1a31): undefined reference to af::array::~array()
Naturally I noticed that I haven't linked arrayfire libraries in my makefile.
However, all my attempts to link the libraries have failed. Can someone please tell what command should I add to link the proper libraries
The files in the lib folder are:
libafcpu.so
libafcuda.so
libafopencl.so
libclBLAS.so
libclBLAS.so.2
libclBLAS.so.2.3.0
libclFFT.so
libclFFT.so.2
libclFFT.so.2.3.0
libStatTimer.so
libStatTimer.so.2
libStatTimer.so.2.3.0
After installation, add the path to the shared libraries of ArrayFire to your LD_LIBRARY_PATH variable. Once you do that, it is merely indicating the correct backend library you need with -l option. Say for cpu, it would be -lafcpu.
What command are you trying ?

Boost undefined reference during compiling

I am getting a compile error trying to compile a simple tester program from the documentation.
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xa6): undefined reference to `_imp___ZN5boost6thread4joinEv'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xb4): undefined reference to `_imp___ZN5boost6threadD1Ev'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xcf): undefined reference to `_imp___ZN5boost6threadD1Ev'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp: (.text$_ZN5boost11this_thread18interruptible_waitEy[boost::this_thread::interruptible_wait( unsigned long long)]+0x4a): undefined reference to `_imp___ZN5boost11this_thread18interruptible_waitEPvNS_6detail7timeoutE'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp: (.text$_ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thre ad_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x23): undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2: ld returned 1 exit status
I am using mingw 4.5 and g++ 4.5.2 on windows. Boost version v1.4.8.
I hope someone can help me solve this problem.
Thanks.
It looks like you aren't linking to the boost libraries.
Boost doesn't come with windows since it isn't a standard library. You've got to download the headers and libraries, then include the headers in your project and link to the libraries at compile time. Since you're using g++, this means adding a -l line to your compile command. The -l line must be used with each specific library you want to use also, you can't just specify the boost directory.
This page will help you get started on Windows and this page will help you get started on *nix platforms.
Once you've compiled boost, then in your example, you should compile your program with
g++ -o tester.exe -Lpath/to/boost/libraries/ -lboost_thread tester.c
Make sure you've got all your libraries linked properly.
Try putting this line first if your thread library is statically defined
#define BOOST_THREAD_USE_LIB
Also, check out this thread.