undefined referance to LibSerial - c++

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1

This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o

Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a

old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.

In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.

Related

why I use the static library to compile and link is corrent while the dynamic library is wrong for am ace demo?

my gcc/g++ version is 4.1.2 , ACE-6.10 in CentOS 5.10 and I make the ACE Library with static_libs=1 option to get the static library, after make and make install ,I get such libraries such as libACE.so , libACE.a and so on, and then I write following code to test, The codes shows as follow:
#include <ace/Log_Msg.h>
#include <ace/OS_main.h>
using namespace std;
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
ACE_DEBUG( (LM_DEBUG, ACE_TEXT("Hello World!\n") ) );
return 0;
}
then I use the following two method to compile and link :
method 1:
g++ -p -o acetest acetest.cpp /usr/local/src/ACE_wrappers/lib/libACE.a -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
method 2:
[root#localhost testCode]# g++ -p -o acetest acetest.cpp -L/usr/local/src/ACE_wrappers/lib -lACE -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
/tmp/cc0eKwlC.o: In function `main':
acetest.cpp:(.text+0x15): undefined reference to `ACE_Log_Msg::last_error_adapter()'
acetest.cpp:(.text+0x1d): undefined reference to `ACE_Log_Msg::instance()'
acetest.cpp:(.text+0x3f): undefined reference to `ACE_Log_Msg::conditional_set(char const*, int, int, int)'
acetest.cpp:(.text+0x57): undefined reference to `ACE_Log_Msg::log(ACE_Log_Priority, char const*, ...)'
collect2: ld θΏ”ε›ž 1
And it comes to the question, the method 1 to use static library is corrent, why method 2,which uses the dynamic library, is wrong?
hunger for the answer, thanks all;
When you compile your application you should add the -DACE_AS_STATIC_LIBS flag to the compiler to indicate that you want to link statically with ACE
Try adding no_hidden_visibility=1 to your platform_macros.GNU file. I believe ACE builds its shared libraries with symbols hidden by default.
See here for the benefits this can provide. However, it doesn't seem to work well when mixing static and dynamic libraries. If anyone has more info on why that is, feel free to chime in.

How to use CityHash128 in c++ code?

I am trying to use google's cityhash hashing function. I am unable to link it to my c++ code. I have installed cityHash and it has generated libcityhash.la, etc files in my /usr/local/lib.
I am setting LD_LIB_LIBRARY=/usr/local/lib, but it doesn't seem to link to these files.
CODE:
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
std::ifstream file("dev/urandom");
char buff[4096];
file.read(buff, 4096);
const uint128 hashed = CityHash128(buff,4096);
file.close();
}
Compiling:
g++ -o city cityHash.cpp
Error:
/tmp/cctSoHTX.o: In function main:
cityHash.cpp:(.text+0x73): undefined reference to `CityHash128(char const*, unsigned long)'
collect2: error: ld returned 1 exit status
I include "city.h" and trying to compile it as follows:
g++ -I /usr/local/include/ -L/usr/local/lib -llibcityhash.a cityHash.cpp -o city
But i m still getting :undefined reference to `CityHash128(char const*, unsigned long)' –
Ok, it's the good old "order makes a difference". Instead of:
g++ -I /usr/local/include/ -L/usr/local/lib /usr/local/lib/libcityhash.a cityHash.cpp -o city
you should do:
g++ -I /usr/local/include/ -L/usr/local/lib cityHash.cpp -o city -lcityhash
(libraries and object files are processed in the order of appearance in the command line, and since none of the code so far has used anything from the library when you list it, nothing gets include from that library - then when you get to the actual code that does use it, you don't give the linker the library after it, so it can't find the symbol - note that this is dependant on the behaviour of the linker, so the same rules may not apply in for example a MS Visual Studio compiler/linker setup)

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]

How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
then I ran it in my RHEL 6 machine with the following command
gcc hi.cpp
and I got some errors which are as follows:
[chankey#localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): 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> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): 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> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey#localhost ~]$
What do these errors denote? My code is correct then why am I getting errors?
Use g++
g++ -o hi hi.cpp
g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.
As the other answers say, use g++ instead of gcc.
Or use make: make hi
You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):
g++ -O4 -Wall hi.cpp -o hi.out
^^^^^ ^^^^^^
optimize related to coding mistakes
For more detail you can refer to man g++ | less.
Try this:
g++ -o hi hi.cpp
gcc is only for C
For a simple hello-world project, calling the compiler directly with g++ command or creating a make file are good options as already answered:
g++ -o hi hi.cpp
or
# After creating the makefile
make hi
For serious projects, however, the usage of a project manager is required. At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).
Following is a simple CMake example:
Make sure you installed cmake on your linux distribution apt-get install cmake or yum install cmake.
Create a file CMakeLists.txt (the name is important) together with your source hi.cpp
project("hi")
add_executable( hi hi.cpp )
Then compile and run as:
cmake -B <path_to_build_folder> -S <path_to_source_folder>
cmake --build <path_to_build_folder>
cd <path_to_build_folder>; ./hi
This allows the project to scale easily with libraries, sources, unit-tests, and much more. It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)
You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.
cmake -G Xcode . #will generate `hi.xcodeproj` you can load on macOS
$ g++ 1st.cpp -o 1st
$ ./1st
if you found any error then first install g++ using code as below
$ sudo apt-get install g++
then install g++ and use above run code
g++ -o foo foo.cpp
g++ --> Driver for cc1plus compiler
-o --> Indicates the output file (foo is the name of output file here. Can be any name)
foo.cpp --> Source file to be compiled
To execute the compiled file simply type
./foo
To Compile your C++ code use:-
g++ file_name.cpp -o executable_file_name
(i) -o option is used to show error in the code
(ii) if there is no error in the code_file, then it will generate
an executable file.
Now execute the generated executable file:
./executable_file_name