Boost not Linking properly using CMake on Ubuntu - c++

I've tried to make the code as simple as possible and I'm still getting
undefined reference to `boost::log::v2_mt_posix:: . . . etc.
This is driving me crazy, so I'm posting my code:
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(BoostTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -DBOOST_LOG_DYN_LINK")
set(SOURCE_FILES
Logger.cpp
Logger.h
loggertest.cpp)
set(BOOST_ROOT /usr/local)
find_package(Boost 1.57.0 COMPONENTS log thread system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest ${SOURCE_FILES})
target_link_libraries(BoostTest ${Boost_LIBRARIES})
loggertest.cpp
#include "Logger.h"
using namespace idair;
int main(int argc, char* argv[]) {
Logger log;
log.trivialLogging();
return 0;
}
Logger.h
#ifndef ONEPRINT_LOGGER_H
#define ONEPRINT_LOGGER_H
#include <boost/log/core/core.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/core/null_deleter.hpp>
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sources = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace idair {
enum severity_level
{
normal,
warning,
error,
critical
};
class Logger {
public:
void trivialLogging();
};
}
#endif //ONEPRINT_LOGGER_H
Logger.cpp
#include "Logger.h"
using namespace idair;
void Logger::trivialLogging() {
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}
ERRORS
Scanning dependencies of target BoostTest
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/Logger.cpp.o
[100%] Building CXX object CMakeFiles/BoostTest.dir/loggertest.cpp.o
Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `idair::Logger::trivialLogging()':
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
CMakeFiles/BoostTest.dir/Logger.cpp.o:/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: more undefined references to `boost::log::v2_mt_posix::trivial::logger::get()' follow
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record::reset()':
/usr/local/include/boost/log/core/record.hpp:153: undefined reference to `boost::log::v2_mt_posix::record_view::public_data::destroy(boost::log::v2_mt_posix::record_view::public_data const*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_composite_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex>, boost::log::v2_mt_posix::sources::features<boost::log::v2_mt_posix::sources::severity<boost::log::v2_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:456: undefined reference to `boost::log::v2_mt_posix::core::get_logging_enabled() const'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::~record_pump()':
/usr/local/include/boost/log/sources/record_ostream.hpp:278: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>&, boost::log::v2_mt_posix::record&)':
/usr/local/include/boost/log/sources/record_ostream.hpp:258: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2_mt_posix::record&)'
/usr/local/include/boost/log/sources/record_ostream.hpp:259: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::auto_release::~auto_release()':
/usr/local/include/boost/log/sources/record_ostream.hpp:243: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2_mt_posix::aux::stream_provider<char>::stream_compound*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::sources::aux::severity_level<boost::log::v2_mt_posix::trivial::severity_level>::set_value(boost::log::v2_mt_posix::trivial::severity_level)':
/usr/local/include/boost/log/sources/severity_feature.hpp:135: undefined reference to `boost::log::v2_mt_posix::sources::aux::get_severity_level()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:259: undefined reference to `boost::log::v2_mt_posix::core::open_record(boost::log::v2_mt_posix::attribute_set const&)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::core::push_record(boost::log::v2_mt_posix::record&&)':
/usr/local/include/boost/log/core/core.hpp:308: undefined reference to `boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&)'
collect2: error: ld returned 1 exit status
make[3]: *** [BoostTest] Error 1
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
make: *** [BoostTest] Error 2

It looks like you installed a static version of boost.
The boost::log::v2_mt_posix namespace is implemented in dynamically built versions of boost (see this link). Your application will attempt to link against symbols from this namespace if you define the BOOST_LOG_DYN_LINK macro during compilation.
If you have the statically built version of Boost.Log installed to /usr/local, try removing the -DBOOST_LOG_DYN_LINK from your ${CMAKE_CXX_FLAGS} line, regenerate your build directory and attempt the build again. (Side note: typically you will add compiler definitions with the add_definitions() command, see this link)
Or, reinstall a dynamic version of boost.

Related

CMakelists.txt for compiling HDF5?

I am trying to compile a simple program to read a HDF5 file. The code compiles correctly with h5c++. However I need a cmakelists.txt for the same
readdata.cpp
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
I tried a cmakelists for it but it didnt work. It gave "not defined errors"
readdata.cpp:(.text+0x1d): undefined reference to `H5::FileAccPropList::DEFAULT'
readdata.cpp:(.text+0x24): undefined reference to `H5::FileCreatPropList::DEFAULT'
readdata.cpp:(.text+0x38): undefined reference to `H5check_version'
readdata.cpp:(.text+0x54): undefined reference to `H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
readdata.cpp:(.text+0x60): undefined reference to `H5::H5File::~H5File()'
CMakelists.txt
cmake_minimum_required(VERSION 3.1.0)
PROJECT (readhdf5)
find_package(HDF5 REQUIRED)
include_directories(${HDF5_INCLUDE_DIRS})
add_executable( readdata readdata.cpp )
target_link_libraries( readdata ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
If i put the HDF5_CXX_LIBRARIES and HDF5_LIBRARIES manually the it works.
target_link_libraries( readdata libhdf5.so libhdf5_cpp.so)
So it is not able to read $HDF5_CXX_LIBRARIES and $HDF5_LIBRARIES.How can I fix this?
The code you attempt to compile depends on the HDF5 C++ bindings, which are not searched for by CMake's HDF5 module by default. Explicitly add the binding to the find_package command:
find_package(HDF5 REQUIRED COMPONENTS C CXX)

Errors linking to Boost system

I've built Boost 1.59 for MinGW 4.9.2 32bit as follows:
bootstrap.bat mingw
b2 --prefix=%USERPROFILE%\Code\Libraries\boost toolset=gcc install (these are static libraries)
When trying to compile:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
cout << "Hello World!" << endl;
return 0;
}
With:
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -l boost_system-mgw49-mt-1_59 -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp
I get this error:
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x193): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x19d): undefined reference to `boost::system::generic_category()'
C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o:main.cpp:(.text+0x1a7): undefined reference to `boost::system::system_category()'
C:/Program Files (x86)/QT/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\Brady\AppData\Local\Temp\ccsbGjrk.o: bad reloc address 0xe in section `.text$_ZN5boost6system14error_categoryD2Ev[__ZN5boost6system14error_categoryD2Ev]'
collect2.exe: error: ld returned 1 exit status
I'm getting this same error across 2 computers. Any help is appreciated!
It's a linker error. You need to link to the Boost System library.
Do this
g++ -o test -L %USERPROFILE%\Code\Libraries\boost\lib -lboost_system -I %USERPROFILE%\Code\Libraries\boost\include\boost-1_59 main.cpp

c++11 threads, SFML and code::blocks

I'm trying to run this code using c++11 std::thread and the SFML library for networking, however I just cant get it to build. I'm pretty sure it has something to do with how I'm setting up the compiler, but I don't know... How can I get this to work? I've been messing about trying to get this to work, going through tutorials and such for hours now, so I have turned to Stack Overflow for answers. I'm using the code::blocks IDE and the x32-4.8.1-posix-dwarf-rev5 compiler on a windows7 64-bit machine.
#include <iostream>
#include <thread>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
void doNetwork() {
cout << "Starting..." << endl;
sf::TcpListener listener;
listener.listen(88);
// Wait for a connection
sf::TcpSocket socket;
listener.accept(socket);
cout << "New client connected: " << socket.getRemoteAddress() << endl;
// Receive a message from the client
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "The client said: " << buffer << endl;
// Send an answer
string message = "Welcome, client";
socket.send(message.c_str(), message.size() + 1);
}
int main()
{
thread task(doNetwork);
task.join();
return 0;
}
This is what the build debug log is saying:
-------------- Build: Debug in bang ---------------
Compiling: main.cpp
Linking console executable: bin\Debug\bang.exe
obj\Debug\main.o: In function `Z9doNetworkv':
C:/Users/user/CppProjects/bang/main.cpp:12: undefined reference to `sf::TcpListener::TcpListener()'
C:/Users/user/CppProjects/bang/main.cpp:13: undefined reference to `sf::TcpListener::listen(unsigned short)'
C:/Users/user/CppProjects/bang/main.cpp:16: undefined reference to `sf::TcpSocket::TcpSocket()'
C:/Users/user/CppProjects/bang/main.cpp:17: undefined reference to `sf::TcpListener::accept(sf::TcpSocket&)'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::TcpSocket::getRemoteAddress() const'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::operator<<(std::ostream&, sf::IpAddress const&)'
C:/Users/user/CppProjects/bang/main.cpp:23: undefined reference to `sf::TcpSocket::receive(void*, unsigned int, unsigned int&)'
C:/Users/user/CppProjects/bang/main.cpp:28: undefined reference to `sf::TcpSocket::send(void const*, unsigned int)'
obj\Debug\main.o: In function `ZN2sf11TcpListenerD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpListener.hpp:43: undefined reference to `sf::Socket::~Socket()'
obj\Debug\main.o: In function `ZN2sf9TcpSocketD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpSocket.hpp:46: undefined reference to `sf::Socket::~Socket()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
10 errors, 0 warnings
The settings I am using with the compiler:
Compiler Flags:
-std=C++0x
Other options:
-std=gnu++0x -U__STRICT_ANSI__
defines:
SFML_STATIC
Linker Settings:
sfml-system-s-d
sfml-network-s-d
Search Directories - Compiler:
C:\Users\user\CppProjects\SFML-2.1\include
C:\Users\user\CppProjects\bang\include
Search Directories - Linker:
C:\Users\user\CppProjects\SFML-2.1\lib\
Toolchain Executables:
-Compilers instalation dir:
C:\Program Files (x86)\mingw-builds\x32-4.8.1-posix-dwarf-rev5\mingw32
-C compiler:
i686-w64-mingw32-gcc-4.8.1.exe
-C++ Compiler:
i686-w64-mingw32-c++.exe
-Linker for dynamic libs:
i686-w64-mingw32-c++.exe
-Linker for static libs:
ar.exe
-Debugger:
gdb.exe
-Resource Compiler:
windres.exe
-Make Program:
mingw32-make.exe
I had a simular problem and how i solved this on a Linux system was: Project / build options
Go to linker settings tab.
there i added: sfml-network
Done.
Hope it helps

xerces c++ and cmake

i tryng to build a small example of Xerces with xerces c++ 3.1 and cmake, but i and only getting linkings problems.
This is my cmkelists.txt:
//============================================================================
project(ConfiguradorXerces)
cmake_minimum_required(VERSION 2.8)
include_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/.libs)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/)
set ( XercesLib xerces-c )
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${XercesLib})
//==============================================
//===============================================
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
using namespace xercesc;
using namespace std;
int main()
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
//==============================================
and this is my console output:
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x25): undefined reference to `xercesc_3_1::XMLUni::fgXercescDefaultLocale'
main.cpp:(.text+0x2a): undefined reference to `xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*)'
main.cpp:(.text+0x2f): undefined reference to `xercesc_3_1::XMLPlatformUtils::Terminate()'
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_3_1::XMLException'
collect2: error: ld returned 1 exit status
make[2]: *** [ConfiguradorXerces] Error 1
make[1]: *** [CMakeFiles/ConfiguradorXerces.dir/all] Error 2
make: *** [all] Error 2
16:28:55: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ConfiguradorXerces (target: Desktop)
When executing step 'Make'
//
i was thinking trhat my cmakeLsits.txt was not complete, there is an especial setup that it has to be done??
thx in advance
I am pretty sure that target_link_libraries() macro accepts a target as its first parameter:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
And you have forgot to specify it. So instead of target_link_libraries(${XercesLib}), try target_link_libraries(${PROJECT_NAME} ${XercesLib}).
Hopefully, that solves it.

Nasty error when initializing vector

I am just trying to make a vector, but it gives me a huge error and I am following a working example from my other project. The code:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct organism {
bool One;
bool Two;
};
std::vector<organism> organisms;
int main() {
printf("Content-type: text/html\n\n");
printf("TEST");
printf(getenv("QUERY_STRING"));
return 0;
}
The error:
> "make"
C:/MinGW/bin/gcc.exe -o build/e2.exe source/main.cpp
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.text$_ZN9__gnu_cxx13new_allocatorI8organismE10deallocateEPS1_j[__gnu_cxx::new_allocator<organism>::deallocate(organism*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt12_Vector_baseI8organismSaIS0_EED2Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt6vectorI8organismSaIS0_EED1Ev+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
"make": *** [build] Error 1
> Process Exit Code: 2
> Time Taken: 00:01
I can compile it if I comment out std::vector<organism> organisms; but I have no clue what's wrong with that line. It's exactly the same in my other project, which compiles fine.
You need to compile with g++.exe instead of gcc.exe so that it will know it needs to link with the C++ library.