Having trouble including boost binary_oarchive.hpp - c++

I have boost installed and I am trying to use boost oarchive in my C++ program
I have binary_oarchive included at the top
#include <boost/archive/binary_oarchive.hpp>
But I guess a multitude of undefined reference errors such as these.
/tmp/ccqt0MAu.o: In function `boost::archive::detail::common_oarchive<boost::archive::binary_oarchive>::~common_oarchive()':
/usr/include/boost/archive/detail/common_oarchive.hpp:35: undefined reference to `boost::archive::detail::basic_oarchive::~basic_oarchive()'
/tmp/ccqt0MAu.o: In function `boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::char_traits<char> >::~binary_oarchive_impl()':
/usr/include/boost/archive/binary_oarchive_impl.hpp:34: undefined reference to `boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::char_traits<char> >::~basic_binary_oprimitive()'
I was wondering what we I could be missing.

These are linker errors and indicate that you aren't linking to the boost serialization library. The command to do this varies based on your platform, but it could be -lboost_serialization.

Related

How to fix undefined references after libtorch custom build?

I've built libtorch from source (as shared objects) with the purpose of slimming it down, but now when I try to link it into an application, I get this:
error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
...
error: undefined reference to 'torch::serialize::OutputArchive::write(std::string const&, at::Tensor const&, bool)'
This doesn't happen when I link with the corresponding pre-built libtorch instead. My application links with -lstdc++ -ltorch -ltorch_cpu -lc10. I've noticed that
Some of these undefined references are from std, others from torch. This surprises me - how is it possible I'm missing stuff from std when I'm linking in stdc++?
The pre-built libtorch includes several .so's I don't have, such as glom. None of these seem necessary to my application though, and I don't need to link them in when using pre-built libtorch.
So what might be the reason I'm getting these undefined references, or how can I go about debugging it?

How to use STL on ARM Cortex-M chips?

I am using STM32 dev board with MCU G++ Compiler & Linker: arm-none-eabi-g++. However, it seems not compatible with STL:
#include <list>
int main (void)
{
std::list<int> list;
list.push_back(1);
list.sort();
return 0;
}
The linker error messages:
abort.c:(.text.abort+0xa): undefined reference to `_exit'
fstatr.c:(.text._fstat_r+0x10): undefined reference to `_fstat'
signalr.c:(.text._kill_r+0x10): undefined reference to `_kill'
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
writer.c:(.text._write_r+0x12): undefined reference to `_write'
closer.c:(.text._close_r+0xc): undefined reference to `_close'
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
lseekr.c:(.text._lseek_r+0x12): undefined reference to `_lseek'
readr.c:(.text._read_r+0x12): undefined reference to `_read'
The C++ STL seems dependent on operating systems. Since the micro-controller has no such things, those essential parts are missing while linking ELF.
The problem is how can I use STL on STM32 L4-series chips?
This problem can be solved by adding -specs=nosys.specs to the G++ linker. This links in a separate library with implementations for all required system functions.
See also: http://pabigot.github.io/bspacm/newlib.html
The C Standardlibrary needs some basic functions (called stubs) to work properly. Normally the OS provides these functions.
-specs= nosys.specs provides very dumb versions of these functions. In your compiler path under /share/doc/gcc-arm-none-eabi/pdf there should be a pdf libc.pdf there you will find some information how to implement those stubs yourself (chapter Systemcalls).

Why will I have C++ unknown reference link error on the application program but not the library in linux

Now I am building a C++ dynamic library libabc.so and an application test based on this library in linux. libabc.so will invoke boost dynamic library libboost.so. I can compile libabc.so very well, and no errors can be found. If I use ldd command on libabc.so, I can easily observe that this library has a dependency on libboost.so. However, when I compile the application program test, I have the following link error:
abc.so: undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
collect2: ld returned 1 exit status
I do not know where the problem comes from. When I compile test program, I am sure that I link it with both libabc.so and libboost.so. I also changed the sequence of libabc.so and libboost.so when linking just to make sure that the right library sequence is given. Any ideas? Thanks.
The function in question is defined to take different arguments, depending on whether the programis compiled as C++03 or C++11.
void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option::enum_type, boost::system::error_code*) // pre-C++11
void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*) // C++11
In my opinion this is a Boost bug.
Consequently, if this function is used in a program, then both Boost and the program must be compiled with C++11 support, or both without. Otherwise the program will fail at the linking stage.
Shared libraries in Linux are normally allowed to have unresolved references, this doesn't cause their linking to fail.

extreme newbie - hello world error

Using B. Stroustrup's Programming text and the 'hello world' program about pg 50 gives errors. I have issues with the "std_lib_facilities.h" include file.
After running (as root) 'gcc hworld1.cpp' the output is -
In file included from /usr/include/c++/4.4/ext/hash_map:60,
from std_lib_facilities.h:34,
from hworld1.cpp:1:
/usr/include/c++/4.4/backward/backward_warning.h:28: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.
/tmp/ccpwXUYx.o: In function `main':
hworld1.cpp:(.text+0x14): undefined reference to `std::cout'
hworld1.cpp:(.text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccpwXUYx.o: In function `__static_initialization_and_destruction_0(int, int)':
hworld1.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
hworld1.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccpwXUYx.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
any suggestions how to fix the file, perhaps commenting out the hash portion?
Excited to finally have time to start, but this seems like a little larger than a 1st troubleshooting task. I have tried just using iostream as the include. I'm running this on Ubuntu 11.04. Perhaps I need to update gcc or use g++. Not sure what might ease me past this one. I tried a few things with 'using ... std' with the ... as the appropriate name I cannot recall (oops). Anyone just know the right includes.
here is the code -
#include "std_lib_facilities.h"
int main()
{
cout<<"hiya people\n";
return 0;
}
You should use g++, because gcc is C compiler (not C++).
This should fix it.
#include <iostream>
int main()
{
std::cout <<"hello world!" << std::endl;
return 0;
}
That said but the book may want you to do it certain way for a reason. Those error are link time errors, after the code has been compiler it needs linking with other procompiled code along with the header file there should be a lib file. you need to add this as a compiler parameter.
EDIT: After further inspection it seems that the missing "link" is the standard library, which isn't surprising seeing as you are using gcc rather than g++ which will automatically link with the stdlib.
Use header file iostream and not the one you are using.
Also make sure you use g++ and not gcc. Since gcc is compiler for C and g++ is for C++

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.