How to build SYCL programs using DPC++ & CMake? - c++

Background
I'm trying to learn SYCL using CUDA backend (I compiled dpc++ compiler using these instructions and vector addition worked). However, the next day I couldn't get the first example from the book to work by using CMake, however using command line and invoking compiler directly solved the problem.
#include <CL/sycl.hpp>
#include <iostream>
#include <string>
using namespace sycl;
const std::string secret =
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01";
const auto sz = secret.size();
int main() {
queue Q;
char* result = malloc_shared<char>(sz, Q);
std::memcpy(result, secret.data(), sz);
Q.parallel_for(sz, [=](auto& i) {
result[i] -= 1;
}).wait();
std::cout << result << '\n';
}
I use this script to setup environment variables:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/shino/software/sources/llvm/build/lib/
export DPCPP_HOME=/home/shino/software/sources
export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/
export CXX=/home/shino/software/sources/llvm/build/bin/clang++
export CC=/home/shino/software/sources/llvm/build/bin/clang
export CUDA_PATH=/usr/local/cuda
And here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(sycl-convolution)
add_executable(convolution main.cpp)
target_include_directories(convolution PRIVATE /home/shino/software/sources/llvm/sycl/include)
target_compile_features(convolution PRIVATE cxx_std_17)
target_compile_options(convolution PRIVATE -fsycl -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=$ENV{CUDA_PATH})
target_link_directories(convolution PRIVATE /home/shino/software/sources/llvm/build/lib/)
Here are the first few lines of linker errors:
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main':
main-4cf7ed.cpp:(.text+0x122): undefined reference to `cl::sycl::event::wait()'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main::{lambda(auto:1&)#1} cl::sycl::queue::submit<cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}>(cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}, cl::sycl::detail::code_location const)':
main-4cf7ed.cpp:(.text+0x3c0): undefined reference to `cl::sycl::event::event()'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x3cc): undefined reference to `cl::sycl::queue::is_host() const'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x47b): undefined reference to `cl::sycl::queue::submit_impl_and_postprocess(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&, std::function<void (bool, bool, cl::sycl::event&)> const&)'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x543): undefined reference to `cl::sycl::queue::submit_impl(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&)'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `void cl::sycl::handler::parallel_for_lambda_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1})':
main-4cf7ed.cpp:(.text+0xa23): undefined reference to `cl::sycl::handler::GetRangeRoundingSettings(unsigned long&, unsigned long&, unsigned long&)'
How do I fix this? My gut feeling tells me that compiler invokes linker differently when invoked directly and I'm not specifying some very important part of linkage process.
Invoking the compiler with verbose mode gave the following link command:
"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/home/shino/software/sources/llvm/build/bin/../lib -L/lib -L/usr/lib /tmp/main-7d74d3.o /tmp/a-cacdd3.o -lstdc++ -lm -lgcc_s -lgcc -lsycl -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o
Other than linker errors, there were a lot more warnings, which suggests than the compiler is not in the correct "mode". I do not know where to start digging next.

You're missing the actual libraries to link with, specify them with target_link_libraries directive. Find more information inside CMake's documentation.
Note that target_link_directories is not enough - it only specifies the paths of the libraries the linker should search for.
More specifically, from reading the link command you've posted, you should add:
target_link_libraries(convolution PRIVATE sycl)
Addition by the OP: Adding SYSTEM to the target_include_directories directive will silence the warnings:
target_include_directories(convolution SYSTEM PRIVATE /home/shino/software/sources/llvm/sycl/include)

Related

boost: mingw: windows 10: undefined reference

How do I add boost logging to a c++ project?
I'm trying to add Boost to my home development environment for the first time. All I want to do right now is be able to use boost's logging implementation to log some errors I'm currently printing to stdout to a file. I started by getting the lambda sample code to build and now I'm trying to add the logging sample code but I'm getting this error:
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:597: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
My code is:
/* for logging */
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
namespace logging = boost::log;
void init(){
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
If I comment out 'logging::add_file_log("sample.log");' it builds just fine
Full build output:
22:15:12 **** Rebuild of configuration Debug for project foo ****
Info: Internal Builder is used for build
g++ -std=c++1y "-IC:\\boost_1_62_0" "-IC:\\boost_1_62_0\\stage\\lib" -O0 -g3 -Wall -c -fmessage-length=0 -DBOOST_LOG_USE_NATIVE_SYSLOG -lboost_filesystem -lboost_system -lboost_thread-mt -lboost_log_setup -lboost_log -o foo_main.o "..\\foo_main.cpp"
g++ -static-libgcc -static-libstdc++ -static "-LC:\\boost_1_62_0\\stage\\lib" -lboost_regex-mgw53-mt-d-1_62 -LC:/boost_1_62_0/stage/lib/ -lboost_regex-mgw53-mt-d-1_62 -o foo.exe foo_main.o -lboost_atomic-mgw53-mt-d-1_62 -lboost_chrono-mgw53-mt-d-1_62 -lboost_container-mgw53-mt-d-1_62 -lboost_context-mgw53-mt-d-1_62 -lboost_coroutine-mgw53-mt-d-1_62 -lboost_date_time-mgw53-mt-d-1_62 -lboost_exception-mgw53-mt-d-1_62 -lboost_filesystem-mgw53-mt-d-1_62 -lboost_graph-mgw53-mt-d-1_62 -lboost_iostreams-mgw53-mt-d-1_62 -lboost_locale-mgw53-mt-d-1_62 -lboost_log_setup-mgw53-mt-d-1_62 -lboost_log-mgw53-mt-d-1_62 -lboost_iostreams-mgw53-mt-d-1_62 -lboost_math_tr1f-mgw53-mt-d-1_62 -lboost_math_tr1l-mgw53-mt-d-1_62 -lboost_math_tr1-mgw53-mt-d-1_62 -lboost_prg_exec_monitor-mgw53-mt-d-1_62 -lboost_program_options-mgw53-mt-d-1_62 -lboost_system-mgw53-mt-d-1_62 -lboost_random-mgw53-mt-d-1_62 -lboost_serialization-mgw53-mt-d-1_62 -lboost_signals-mgw53-mt-d-1_62 -lboost_system-mgw53-mt-d-1_62 -lboost_test_exec_monitor-mgw53-mt-d-1_62 -lboost_thread-mgw53-mt-d-1_62 -lboost_timer-mgw53-mt-d-1_62 -lboost_type_erasure-mgw53-mt-d-1_62 -lboost_unit_test_framework-mgw53-mt-d-1_62 -lboost_wave-mgw53-mt-d-1_62 -lboost_wserialization-mgw53-mt-d-1_62
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `make_absolute':
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:597: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost3log10v2s_mt_nt55sinks17text_file_backend30set_file_name_pattern_internalERKNS_10filesystem4pathE':
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:1264: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem6existsERKNS0_4pathE':
...enter code here
C:\boost_1_62_0/./boost/filesystem/operations.hpp:872: undefined reference to `boost::filesystem::detail::dir_itr_close(void*&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem18directory_iteratorC1ERKNS0_4pathE':
C:\boost_1_62_0/./boost/filesystem/operations.hpp:903: undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem18directory_iterator9incrementEv':
C:\boost_1_62_0/./boost/filesystem/operations.hpp:939: undefined reference to `boost::filesystem::detail::directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem4pathaSINS0_15directory_entryEEENS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIT_E4typeEEERS1_E4typeERKS8_':
C:\boost_1_62_0/./boost/filesystem/path.hpp:202: undefined reference to `boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)'
collect2.exe: error: ld returned 1 exit status
22:15:18 Build Finished (took 5s.607ms)
I am doing this on a windows 10 machine running Eclipse Version: Neon.1a Release (4.6.1). I am using a mingw compiler, gcc (GCC) version 5.3.0.
I built my boost libraries using this command:
b2 toolset=gcc cxxflags=-std=c++1y -a
The absolute path to my boost_filesystem library is:
C:\boost_1_62_0\stage\lib\libboost_filesystem-mgw53-mt-d-1_62.a

Build library libtorrent debian and link it to program c++

I always have problems when building libraries and linking them, so I hope someone can give me a hand.
I downloaded libtorrent from here and I've built it like they explain here in the building with autotools section (skipping step 1). The building process was successfull I think, but when I did make check the output was:
============================================================================
Testsuite summary for libtorrent-rasterbar 1.0.5
============================================================================
# TOTAL: 0
# PASS: 0
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
Maybe it should say: total X?
I did a little program where I added #include <libtorrent/session.hpp> and when I compile with g++ file.cpp -o file it says libtorrent/session.hpp: No such file or directory.
Should I add some flags to g++ such as -lpthread for other projects and thinks like that?
Thanks
UPDATE:
When installing without building using sudo apt-get install libtorrent-rasterbar-dev and compiling my main.cpp file I get this error:
g++ main.cpp -o file
In file included from /usr/include/libtorrent/session.hpp:49:0,
from main.cpp:2:
/usr/include/libtorrent/config.hpp:46:2: error: #error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in order for asio's declarations to be correct. If you're linking dynamically against libtorrent, define BOOST_ASIO_DYN_LINK otherwise BOOST_ASIO_SEPARATE_COMPILATION. You can also use pkg-config or boost build, to automatically apply these defines
#error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in \
UPDATE 2:
Modified the main.cpp file to add the the following above the libtorrent #include directives:
#ifndef BOOST_ASIO_DYN_LINK
#define BOOST_ASIO_DYN_LINK
#endif
But then I have this problem:
$ g++ main.cpp
/tmp/ccM2ItFb.o: In function `main':
main.cpp:(.text+0x57): undefined reference to `libtorrent::default_storage_constructor(libtorrent::file_storage const&, libtorrent::file_storage const*, std::string const&, libtorrent::file_pool&, std::vector<unsigned char, std::allocator<unsigned char> > const&)'
main.cpp:(.text+0xb9): undefined reference to `libtorrent::session::~session()'
main.cpp:(.text+0x105): undefined reference to `libtorrent::session::~session()'
/tmp/ccM2ItFb.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x162): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x16e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x17a): undefined reference to `boost::system::system_category()'
main.cpp:(.text+0x192): undefined reference to `boost::asio::error::get_netdb_category()'
main.cpp:(.text+0x19e): undefined reference to `boost::asio::error::get_addrinfo_category()'
main.cpp:(.text+0x1aa): undefined reference to `boost::asio::error::get_misc_category()'
/tmp/ccM2ItFb.o: In function `boost::asio::error::get_system_category()':
main.cpp (.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccM2ItFb.o: In function `libtorrent::session::session(libtorrent::fingerprint const&, int, unsigned int)':
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x3c): undefined reference to `libtorrent::rel_clocktime_pools_nolog_resolvecountries_deprecated_dht_ext_()'
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x75): undefined reference to `libtorrent::session::init(std::pair<int, int>, char const*, libtorrent::fingerprint const&, int, unsigned int)'
/tmp/ccM2ItFb.o: In function `void boost::checked_delete<libtorrent::torrent_info const>(libtorrent::torrent_info const*)':
main.cpp:(.text._ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_[_ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_]+0x1a): undefined reference to `libtorrent::torrent_info::~torrent_info()'
collect2: error: ld returned 1 exit status
Also tried compiling using: g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp with the same result.
UPDATE 3:
I was able to solve the problem. I had to compile using:
g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp and add also the link library -ltorrent-rasterbar
Sounds like you've compiled the library but not actually installed it anywhere. An #include directive looks in /usr/include by default, but the libtorrent headers are somewhere in the directory where you built the library. You'll need to either install the library's files into the system directories, or give the compiler an -I option pointing to the libtorrent build directory. (You'll probably also need an -L option when linking, for the same reason.)

Poco library linking errors in ubuntu platform

I have downloaded the latest Poco library poco-1.7.3.tar. Configured with few properties and did make install.
Tried the following sample helloworld program.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Compiled it using
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/XML/include -I poco-1.7.3/JSON/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoUtil -lPocoFoundation -lPocoXML -lPocoJSON helloworld.cpp -o prog
But it is throwing following errors
/tmp/ccFvl4ll.o: In function `main':
helloworld.cpp:(.text+0x4f): undefined reference to `Poco::Util::Application::init(int, char**)'
helloworld.cpp:(.text+0xd9): undefined reference to `Poco::Logger::log(Poco::Exception const&)'
/tmp/ccFvl4ll.o: In function `Poco::RefCountedObject::release() const':
helloworld.cpp:(.text._ZNK4Poco16RefCountedObject7releaseEv[_ZNK4Poco16RefCountedObject7releaseEv]+0x6e): undefined reference to `Poco::Bugcheck::unexpected(char const*, int)'
/tmp/ccFvl4ll.o: In function `Poco::Util::Application::logger() const':
helloworld.cpp:(.text._ZNK4Poco4Util11Application6loggerEv[_ZNK4Poco4Util11Application6loggerEv]+0x2c): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationC2Ev[_ZN20HelloPocoApplicationC5Ev]+0x14): undefined reference to `Poco::Util::Application::Application()'
/tmp/ccFvl4ll.o:(.gcc_except_table+0x2c): undefined reference to `typeinfo for Poco::Exception'
/tmp/ccFvl4ll.o: In function `Poco::AutoPtr<HelloPocoApplication>::operator->()':
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3a): undefined reference to `Poco::NullPointerException::NullPointerException(int)'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3f): undefined reference to `Poco::NullPointerException::~NullPointerException()'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x44): undefined reference to `typeinfo for Poco::NullPointerException'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x20): undefined reference to `Poco::Util::Application::name() const'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x28): undefined reference to `Poco::Util::Application::initialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x30): undefined reference to `Poco::Util::Application::uninitialize()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x38): undefined reference to `Poco::Util::Application::reinitialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x40): undefined reference to `Poco::Util::Application::defineOptions(Poco::Util::OptionSet&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x48): undefined reference to `Poco::Util::Application::run()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x50): undefined reference to `Poco::Util::Application::handleOption(std::string const&, std::string const&)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::~HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationD2Ev[_ZN20HelloPocoApplicationD5Ev]+0x1f): undefined reference to `Poco::Util::Application::~Application()'
/tmp/ccFvl4ll.o:(.rodata._ZTI20HelloPocoApplication[_ZTI20HelloPocoApplication]+0x10): undefined reference to `typeinfo for Poco::Util::Application'
collect2: error: ld returned 1 exit status
Can you please help me?
I managed to run exactly that code but using CMake rather than Makefile and it worked. you have just to change the paths according to your machine.
Here's my CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.3)
project(tutocpp14)
set(Poco_DIR "/usr/local/lib/cmake/Poco/")
set(Poco_INCLUDE_DIRS "/usr/include/Poco/")
find_package(Poco REQUIRED COMPONENTS Foundation Net XML Util) # add other components here
# check c++11 / c++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11 " COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
include_directories( ${Poco_INCLUDE_DIRS})
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${Poco_LIBRARIES})
Cheers,
Checking with Poco documents it seems it should work. Looking at an example here, and its makefile, I suspect the order of your library is reversed. Assuming that paths are correct (since ld didn't report on that) I would say that linking order is incorrect. See here Why.
So I suggest you change your command to
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoFoundation -lPocoUtil helloworld.cpp -o prog
in case those examples goes out of scope somehow in future, here is the code and makefile copied.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Note: Remember to restore the tabs instead of spaces in makefile code!
POCO_LIBS=-lPocoFoundation -lPocoUtil
all: hello-poco
clean:
rm -f hello-poco
hello-poco: hello-poco.cpp
$(CXX) $(POCO_LIBS) -o hello-poco hello-poco.cpp
Update:
With all above said, I have to put a note on strangeness of your error especially Poco::Logger which is found in foundation library. So it seems that library does not get linked. I would suggest two things:
1. Check if its not corrupted. You can do that by cleaning and recompiling
2. Try to install in standard path so that you avoid linking paths
I know all these are not definitive (guess works at best) but will be useful to provide you hints of where the issue lies.
Your original library linking order was fine, but you are missing XML and JSON (Util depends on these libraries). Although, the link errors you've got indicate that you may be either missing Foundation library or mixing library versions.
$ echo $POCO_BASE
/tmp/poco
$ pwd
/tmp/poco/test
$ g++ -I $POCO_BASE/Util/include -I $POCO_BASE/Foundation/include -L $POCO_BASE/lib/Linux/x86_64 -lPocoUtil -lPocoXML -lPocoJSON -lPocoFoundation helloworld.cpp -o prog
try this command to compile.
g++ <src_file>.cpp -lPocoFoundation -lPocoJSON -lPocoUtil -lPocoJWT
-o <binary_name>

C++ boost libraries shared_memory_object undefined reference to 'shm_open'

I tried to compile the following code on ubuntu 11.04:
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, "Highscore", boost::interprocess::read_write);
shdmem.truncate(1024);
std::cout << shdmem.get_name() << std::endl;
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << size << std::endl;
}
only to get the following errors:
/tmp/cc786obC.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0xe0): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x116): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x16c): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x1c0): undefined reference to `shm_open'
collect2: ld returned 1 exit status
Command I used to compile the file: g++ -o shared shared.cpp
Command I used to install the boost libraries: sudo apt-get install libboost-dev libboost-doc
shm_open is made available by linking librt. Try passing -lrt flag to the linker.
Try:
g++ -c -Wall shared.cpp
g++ -L /lib -lrt shared.o -o shared
Just adding to #anio's answer:
While linking, the -lrt flag may need to be added at the end of the command.
Try:
g++ -L /lib shared.o -o shared -lrt
My same problem got solved from #anio's answer but I needed to do additional work. As I cannot comment due to low reputation. So I am presenting my pennies, may be someone find it helpful. I am baby stepping everything, so "sorry" if I seem childish.
I am using Eclipse on Debian for cross compiling for arm-linux-gnueabihf-g++. So I first found the location for "librt"
/$ find -iname "librt*"
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.a
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.so
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librtmp.so.0
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt-2.13.so
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt-2.19.so
./lib/i386-linux-gnu/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt-2.19.so
./lib/i386-linux-gnu/librt-2.19.so
As I prefer to sync with the remote target machine I have added "sysroot path" for my library into eclipse project properties "Library Search Path (-L)"
/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf
Also added "rt" to Libraries (-l), which ultimately solved my problem.
In case you are compiling with the use
g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt
replace $YOUR_PATH_TO_LIB with yours.
g++ -L /lib shared.o -o shared -lrt -lpthread

Compiling C++ source file using Boost.Thread

I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example:
#include <boost/thread.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void thread()
{
for(int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << std::endl;
}
}
int main()
{
boost::thread t(thread);
t.join();
}
However, when I compile this with the following from the command line:
$ g++ example61.cpp -o example61 -I /usr/local/include
I get the following output:
/tmp/cc6bVu1F.o: In function `main':
example6.cpp:(.text+0x9d): undefined reference to `boost::thread::join()'
example6.cpp:(.text+0xae): undefined reference to `boost::thread::~thread()'
example6.cpp:(.text+0xc6): undefined reference to `boost::thread::~thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data_base::thread_data_base()':
example6.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc6bVu1F.o: In function `void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)':
example6.cpp:(.text._ZN5boost11this_thread5sleepINS_10posix_time7secondsEEEvRKT_[void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)]+0x35): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/tmp/cc6bVu1F.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
example6.cpp:(.text._ZN5boost6threadC2IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE]+0x30): undefined reference to `boost::thread::start_thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
example6.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc6bVu1F.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
I don't know how to interpret this. Can anyone help? Thank you so much!
That is a linking error. It means your code is correct and you include the correct headers, but the compiler doesn't link against the boost threading library. To fix this, you need to compile like this:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread
If you've installed the Boost threading library to a non-standard path, you must also add it to the search path:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib
You need to link with the library. Some Boost libraries are implemented entirely in the header files and do not need a library. But others, like thread, are implemented partly in headers and partly in compiled library code.
I believe that you need to add -lboost_thread-mt to your compile command.
Boost thread are not a template only library. You need to add a -lboost_thread while linking /compiling.
Most of the libraries in boost are implemented in headers. They can simply be included like you have done. Boost thread on the other hand, is of such a nature that you need to depend on its compiled units, only the declaration of its function are readily available to you in the header. So the compiler, or more correctly the linker, which is responsible for linking your calls to the declared functions /classes need to know where to look for these symbols. By invoking the compiler with a -lboost_thread you tell it to link to the library (-l) boost thread.
Following your comments I share with you compilation string for pocketcpp compilation tool:
g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
Good luck,
I commented above, but wanted to share the full command line here.
g++ -std=c++11 thread_example.cpp -lboost_thread -lboost_system
[I'm using thread_example.cpp as the source filename; please replace with your own]