I have downloaded gtest 1.7.0 sources from here:
https://code.google.com/p/googletest/downloads/list
and build the gtest .a files (lib files) on ubuntu 13.10:
Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
and the resulting lib is called: libgtest.a. In my main.cpp file Have:
#include <iostream>
#include "gtest/gtest.h"
int main(){
std::cout << "Test \n";
int argc = 2;
char* cp01;
char* cp02;
char* argv[] = {cp01, cp02};
testing::InitGoogleTest(&argc, argv);
return 0;
}
From a terminal I build with:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lpthread -lgtest
which gives the following errors:
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
Based on this:
error during making GTest
I have also tried -pthread instead of -lpthread but gives same error:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -pthread -lgtest
EDIT: I have also tried to specifying -pthread as the last argument:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread
same error
What am I doing wrong?
You need to specify -pthread after -lgtest. The linker takes libraries in order, and only takes as much as it needs to resolve references which are undefined at that point.
Nope, the problem is with Gtest's build.
If you build it using the standard configure approach, it isn't supplying the -lpthread correctly to create libgtest.so. Hence, when you try building a final shared library that actually uses the pthread capability it fails.
Instead, use the Cmake approach:
cd gtest-1.7.0
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON ..
make
And then manually install these into /usr/lib/
This version correctly links in libpthread into libgtest.
The option -lgtest is attempting to link the dynamic library libgtest.so. You
wish to link the static library /home/user/gtest-1.7.0/lib/.libs/libgtest.a.
Instead of:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread
use:
g++ main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread
Note that your commandline supplies no name for the resulting executable, which will default
to a.out. If you want it called, e.g. mytest, then do:
g++ -o mytest main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread
Use -pthread instead of -lpthread (for linking with pthread-library), while you using gtest in your executable.
OR
Move the -lpthread after libgtest.a (sequence matters).
To answer we probably need more information, are you on a 64 bit machine and downloaded a 32 bit library?
Related
I installed casacore from source using the GitHub repository on my Ubuntu 18.04. The installation completes without any errors and the respective files are written to the expected directories (the .h files to /usr/local/include & libraries to /usr/local/lib). On trying to compile a basic C++ file using these I'm given the following error:
tmp/ccBxZcf3.o: In function 'main': /home/zealouspriest/C_C++_Projects/bb++/ms.cpp:15: undefined reference to 'casacore::MeasurementSet::MeasurementSet()'
/home/zealouspriest/C_C++_Projects/bb++/ms.cpp:15: undefined reference to 'casacore::MeasurementSet::~MeasurementSet()'
collect2: error: ld returned 1 exit status
The compiler command that I use is as follows:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib -lcasa_casa -lcasa_tables -lcasa_ms ms.cpp -o ms
The ms.cpp file being compiled is extremely simple and just creates an empty measurement set to test for successful linking and is as follows:
//ms.cpp
#include <iostream>
#include </usr/local/include/casacore/ms/MeasurementSets/MeasurementSet.h>
int main(){
casacore::MeasurementSet ms = casacore::MeasurementSet();
return 0;
}
Here is all that I have tried:
a) Building from source using GitHub instructions,
b) Installing from Ubuntu repository.
Thanks in advance for your time!
When compiling manually with g++ you need to first specify your sources, and then the dependencies (libraries):
g++ -o ms ms.cpp -I/usr/local/include -L/usr/local/lib -lcasa_casa -lcasa_tables -lcasa_ms -g -Wall
Better just use CMake if you plan to have something more that just one cpp.
Related topics:
linking files in g++
gcc-g++-parameter-order
Alternatively, you can use the -Wl,--no-as-needed options:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib -Wl,--no-as-needed -lcasa_ms ms.cpp -o ms
I am compiling an opensource project to run on my machine which is this project. It requires boost library so I installed the Boost_1_55 library on my ubuntu machine but the compiling process was not successfully finished by printing out some error messages as follows.
libtool: link: g++ -g -O3 -Wall -DKENLM_MAX_ORDER=6 -W -Wall -Wno-sign-compare -I./.. -pthread -I/usr/include -g -O2 -o .libs/query query_main.o ./.libs/libklm.so ../util/.libs/libklm_util.so -lz -L/usr/lib/x86_64-linux-gnu -lboost_program_options -lboost_thread -lboost_system -lpthread -lrt -pthread
../util/.libs/libklm_util.so: undefined reference to `boost::thread::join()'
../util/.libs/libklm_util.so: undefined reference to `boost::thread::~thread()'
./.libs/libklm.so: undefined reference to `boost::thread::start_thread()'
collect2: ld returned 1 exit status
This answer seems the solution for my problem but the result of ls -al /usr/local/lib | grep thread showed me like below.
libboost_thread.a
libboost_thread.so -> libboost_thread.so.1.55.0
libboost_thread.so.1.49.0
libboost_thread.so.1.55.0
I don't know what else to check more. Thank you in advance for your help.
You can try to add /usr/local/lib to LD_LIBRARY_PATH like this
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
You have the static Boost library object (libboost_thread.so) but do you have the Boost development files installed? Check to see if the /usr/include/boost/thread directory exists and has *.hpp files in it. If not you may need to install the libboost-thread-dev package for your distribution or download the header files directly from Boost.org.
I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.
It was a long long way, but I managed to include a shared file (libpi_pi_gcs2.so) included into C++ code under Linux Mint 17 Cinnamon 64-bit. Now as the dust is saddled I have a question left... let me summarize:
I copied libpi_pi_gcs2.so in usr/local/lib.
Trying to compile main.cpp with
g++ -Wall -o test main.cpp -lpi_pi_gcs2
faild, returning
/usr/bin/ld: skipping incompatible //usr/local/lib/libpi_pi_gcs2.so when searching for -lpi_pi_gcs2
/usr/bin/ld: cannot find -lpi_pi_gcs2
collect2: error: ld returned 1 exit status
I talked with my flatmate, and he guessed maybe the shared library is only 32bit compatible.
Using -m32 let me take a step forward! But yielding new errors.
I googled and it seemed like I needed C32 std libraries. And I installed:
apt-get install ia32-libs
Again the old error messages disappeared and new one arose, after googling around I installed:
apt-get install g++-multilib
Trying to compile it now using
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2
Let to the error messages:
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlsym'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlopen'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlclose'
The solution was the following command:
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2 -ldl
Now my question:
Is it necessary to explicitly state -ldl i.e. link with libdl.so because the -m32 flag declares everything what follows to be 32bit and hence the linker would without explicitly
writing -ldl search for a 32bit libdl.so? I guess mine is 64 bit I am working on a 64bit system.
What do you think? More details and background informations are appreciated.
Greetings,
newandlost
This is my first time trying to make a simple library. I worked in Ubuntu 12.04 with g++ 4.6.3. Here is the problem:
[[mylib.cpp]]
#include<sqlite3.h>
void Mylib::blahblah() {...}
void Mylib::evenmoreblah() {...}
...
[[mylib.h]]
#include <...>
class Mylib {
...
};
Then I made the lib by:
gcc -c -Wall -fpic mylib.cpp
gcc -shared -o libmylib.so mylib.o
I used the library in a single test.cpp which contains only the main(). I put libmylib.so in ./libdir, and compiled by using:
g++ -g test.cpp -o test -lpthread -L/usr/local/lib -lsqlite3 -L./libdir -lmylib
The error I got:
./libdir/libmylib.so: undefined reference to `sqlite3_close'
./libdir/libmylib.so: undefined reference to `sqlite3_exec'
./libdir/libmylib.so: undefined reference to `sqlite3_free'
./libdir/libmylib.so: undefined reference to `sqlite3_open'
You could link -lsqlite3 into your shared library with
gcc -shared mylib.o -o libmylib.so -lsqlite3
If you do that, you don't need to explicitly link -lsqlite3 to your program, but that won't harm.
and the order of linking arguments for your program is important:
g++ -Wall -g test.cpp -o mytest \
-L./libdir -lmylib -L/usr/local/lib -lsqlite3 -lpthread
it should go from higher-level libraries to lower-level (i.e. system) ones. And don't forget -Wall to get almost all warnings from the compiler, which is very useful.
Read the Program Library HowTo.
PS. Don't call your program test which is a shell builtin (and the standard /usr/bin/test). Use some other name.
If your library make references to sqlite3, you should link sqlite after linking your library :
g++ -g test.cpp -o test -lpthread -L/usr/local/lib -L./libdir -lmylib -lsqlite3
Otherwise ld won't find anything useful in libsqlite3 before linking your library and won't be able to find the requested symbols after that.
Since your library uses sqlite3, you need to add that AFTER your own library in the linker command. I think you could add it to the linking of your shared library too, but not certain.
The linker resolves libraries and their references in the order you list them, so the order is important.