I am new to gtest/gmock. I am trying to implement unit testing for C++ Program.but when i try to compile it. i got linking error with gmock. this specific error is related testing::internal::GetCurrentOsStackTraceExceptTop.
Installation of gtest/gmock:
Step 1: sudo apt-get install libgtest-dev
Step 2: download the git repo of google test from here: https://github.com/google/googletest
Step 3: sudo apt install cmake
Step 4: run these commands {~googletest$}
sudo cmake CMakeLists.txt
sudo make
Step 5: copy files in /usr/lib
Step 6: copy gtest/gmock include folder into /usr/local/include
version of the Google Test library that is installed on my system is 1.10.0-2.
test.cpp
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gmock/gmock-actions.h>
#include <iostream>
#include "timeEvent.h"
using ::testing::_;
using ::testing::Return;
class MockStat {
public:
MOCK_METHOD2(stat, int(const char* pathname, struct stat* buf));
};
TEST(MyTest, PositiveCase) {
MockStat mock_stat;
struct stat fileStat;
fileStat.st_mtime = 111;
fileStat.st_ctime = 222;
EXPECT_CALL(mock_stat, stat("/var/lib/systemd/timesync/clock", &fileStat))
.WillOnce(Return(0));
auto latest_clock = get_latest_clock_entry();
EXPECT_EQ(latest_clock.tv_sec, 111);
EXPECT_EQ(latest_clock.tv_nsec, 0);
}
compile: g++ test.cpp library.cpp -lgtest -lgtest_main -lpthread -lboost_thread -pthread -lboost_filesystem -lgtest -lgmock -lgmock_main
Error:
g++ update.cpp library.cpp -lgtest -lgtest_main -lpthread -lboost_thread -pthread -lboost_filesystem -lgtest -lgmock -lgmock_main -lstdc++
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/libgmock.a(gmock-all.cc.o): in function `testing::internal::Log(testing::internal::LogSeverity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
gmock-all.cc:(.text+0x121a): undefined reference to `testing::internal::GetCurrentOsStackTraceExceptTop[abi:cxx11](int)'
collect2: error: ld returned 1 exit status
Looks like you are using gmock/gtest libraries from 2 different sources. The first source is libgtest-dev you installed and the second is github repository. You should use either libgtest-dev or the one from github repo.
testing::internal::GetCurrentOsStackTraceExceptTop[abi:cxx11](int) is defined in libgtest. libgmock depends on libgtest. Therefore -lgtest should follow -lgmock:
g++ update.cpp library.cpp -lboost_thread -pthread -lboost_filesystem -lgmock -lgtest -lgmock_main
Don't use -lstdc++ with g++. It's by default. And a proper way is -stdlib=stdc++.
See Why does the order in which libraries are linked sometimes cause errors in GCC? for more info. Your question can be considered as a dupe.
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 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?
I am trying to install the C++ MongoDB 2.4 driver using this tutorial:
http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-cpp-driver/
I downloaded and built the driver source code successfully using scons.
When I'm trying to compile the sample C++ file using the recommended command:
$ g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system -o tutorial
I get the following error:
/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/libmongoclient.a(sock.o):
In function `mongo::SSLManager::setupPEM(std::string const&, std::string const&)':
(.text+0xc99): undefined reference to `SSL_CTX_use_certificate_chain_file'
/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/libmongoclient.a(sock.o):
In function `mongo::SSLManager::setupPEM(std::string const&, std::string const&)':
(.text+0xceb): undefined reference to `SSL_CTX_set_default_passwd_cb_userdata'
I checked and I have already installed this packages: libssl1.0.0 libssl-dev.
Please help me!
Try compiling with crypto & ssl.
g++ tutorial.cpp -pthread -lmongoclient \
-lboost_thread -lboost_filesystem -lboost_program_options -lboost_system \
-lssl -lcrypto -o tutorial
I have a problem: I wrote code using Boost (locks.hpp). My server is running x64 Ubuntu (Linux). When i compile this code with -m64, it builds fine. But when I try to compile for -m32, I get these errors:
g++ -fPIC -m32 -shared -Wl,-soname,test.so -ldl -o test.so test.cpp -lboost_thread
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libboost_thread.so when searching for -lboost_thread
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libboost_thread.a when searching for -lboost_thread
/usr/bin/ld: skipping incompatible //usr/lib/libboost_thread.so when searching for -lboost_thread
/usr/bin/ld: skipping incompatible //usr/lib/libboost_thread.a when searching for -lboost_thread
/usr/bin/ld: cannot find -lboost_thread
collect2: ld returned 1 exit status
What am I doing wrong? Thanks!
Try installing the 32-bit boost package:
sudo apt-get install libboost-thread-dev:i386
You need a 32 bit version of the thread library. The answer to your question is already on stackoverflow.com. Use the address-model option when you build boost from source. Boost provides great documentation for building on Linux.
bjam address-model=32