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

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.

Related

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)

FFMPEG: undefined reference to `avcodec_register_all' does not link

So I have a very sample code for trying to decode a FFMPEG video stream.
My problem is avcodec does not want to link, to do so I made a clean installation of Ubuntu 13.04. I have build ffmpeg from source following the guide here: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
I just want to compile my file. Note that my ubuntu does not have any implementations or header files for avcodec. The command line I use is:
gcc -I/home/USER/ffmpeg_build/include -L/home/USER/ffmpeg_build/lib -lavcodec -o test.exe Downloads/auv/src/dronerosvideo/src/ar2.cpp
/tmp/ccKTprFq.o: In function `fetch_and_decode(int, int, bool)':
ar2.cpp:(.text+0x36e):
undefined reference to `avcodec_register_all'
ar2.cpp:(.text+0x378):
undefined reference to `av_log_set_level'
ar2.cpp:(.text+0x382):
undefined reference to `avcodec_find_decoder'
ar2.cpp:(.text+0x3b1):
undefined reference to `avcodec_alloc_context3'
ar2.cpp:(.text+0x3d6):
undefined reference to `avcodec_open2'
ar2.cpp:(.text+0x46d):
undefined reference to `av_init_packet'
ar2.cpp:(.text+0x50a):
undefined reference to `avcodec_decode_video2'
ar2.cpp:(.text+0x534):
undefined reference to `av_free_packet'
/tmp/ccKTprFq.o:(.eh_frame+0x13): undefined reference to
`__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Just for a sane test if I remove the -L argument compiler says:
/usr/bin/ld: cannot find -lavcodec
Which means that the linker finds the library in /home/USER/ffmpeg_build/lib. Also if we check the library for implementation it exists:
nm ffmpeg_build/lib/libavcodec.a | grep "register_all"
0000000000000000 T avcodec_register_all
Also as advised since it is C++ I have exten "C" around the include of the library.
At this point I'm falling out of any ideas at all, why exactly compilation fails?
First, it is C++, so you'll need to use g++ not gcc so that the C++ standard library gets linked. This should get rid of undefined reference to '__gxx_personality_v0'.
Then, the order of libaries to link is actually important.
You'll need to specify a library after the object (or source or other library) using it.
Putting it together, a command line like this works (in my tests):
g++ -o test.exe -I$HOME/ffmpeg/include test.cc -L$HOME/ffmpeg/lib -lavcodec
(Actually, depending on how ffmpeg was built, you might need other libraries as well, like pthreads or libx264)
If you got pkg-config installed, it might be possible to just ask that it for proper clags:
# Since you didn't install ffmpeg to a known location, tell pkg-config about that location.
export PKG_CONFIG_PATH=$HOME/ffmpeg/lib/pkgconfig
g++ -o test.exe $(pkg-config -clags libavcodec) test.cc $(pkg-config -libs libavcodec)

Undefined reference to member function found in static library

I know what you're thinking - this has been answered a million times. I wish I could put something in the Title that would make it clear that I already know that the order of libraries is important.
I'm using g++ on Centos 6 64. I have a simple test program:
#include <ptlib.h>
int main()
{
PTimer indirectTimer1_;
indirectTimer1_.SetNotifier(0);
return 0;
}
and I compile and link it with this command:
g++ -I./ptlib/include/ mm.cpp ptlib/lib_linux_x86_64/libpt_s.a -lpthread -lrt
and I get this as an answer back:
/tmp/cc53itXb.o: In function `main':
mm.cpp:(.text+0x52): undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
collect2: ld returned 1 exit status
But I know that PTimer::SetNotifier is in the .a file:
nm -AC ptlib/lib_linux_x86_64/libpt_s.a | grep SetNotifier
ptlib/lib_linux_x86_64/libpt_s.a:osutil.o:0000000000003dd8 T PTimer::SetNotifier(PNotifierTemplate<long> const&)
To make matters more confusing, when I compile the library on Centos 5, 32bit, and run the same test, it links just fine.
I've tried using '-Lptlib/lib_linux_x86_64 -lpt_s' and I've tried using the -Wl,--start-group / -Wl,--end-group args to no avail. If I add 'ptlib/src/ptlib/unix/osutil.cxx' to the g++ line, it compiles and links just fine. Unfortunately, this is but a sample of the unreferenced functions in our main program. Not all of the, just a few seemingly random methods like this one are undefined (which also links just fine on Centos 5, 32 bit).
As you can see, I've tried a lot of things but still haven't figured it out. I need something else to try! Or someone to point out something terribly easy that I am doing wrong.
undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
PTimer::SetNotifier(PNotifierTemplate<long> const&)
Notice the difference in int vs. long?

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]

undefined referance to LibSerial

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.