cannot find -lgtest when setting up Google Test
Asked Answered
S

3

8

I'm using Google Test for C++ and trying to set it up on my linux machine. My make file has the following code:

CC=g++ 
CFLAGS=-I $(GOOGLETESTDIR)/include -L $(GOOGLETESTDIR)/lib -lgtest -lpthread -Wall
DEPS=fib.h
OBJS=fib.o main.o

all: | r6

clean:
    -rm -f r6 $(OBJS)

%.o: %.cpp $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS) 

r6: $(OBJS)
    $(CC) -o $@ $^ $(CFLAGS) 
.PHONY: all clean

I get the error when I run make:

/usr/bin/ld: cannot find -lgtest

How do I fix this? I'm new to this kind of testing and rather new to linux so I'm really lost.

Specify answered 5/3, 2015 at 3:10 Comment(2)
Is the variable $GOOGLETESTDIR set in the shell you are running make from? If so, what are the contents of ${GOOGLETESTDIR}/lib?Libidinous
First of all, you don't need space between -L and path. In order to identify what's the issue, please post g++ output before the error message you posted. In there you should be able to see what's the reslved -L path.Reactance
L
13

I had this issue on Ubuntu 17.10 and basically what Alexander says is true.

Someone wrote a nice tutorial with explicit commands that can be found at https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

It boils down to:

sudo apt install libgtest-dev cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib

Personally, I would appreciate a solution that did not manually move files into /usr/lib, but on the plus side this works as-is.

Lippold answered 4/12, 2017 at 18:21 Comment(1)
sudo ln -s /usr/src/gtest/libgtest_main.a /usr/local/lib/libgtest_main.a sudo ln -s /usr/src/gtest/libgtest.a /usr/local/lib/libgtest.aTight
M
0

As of now, Google test framework is not shipped with prebuilt binaries; you need to build them yourself. See full details on how to do that in README (for Debian, the path is /usr/src/googletest/googletest/README.md).

Mannered answered 29/6, 2017 at 15:35 Comment(1)
debian is following gtest best practices here; for the reason behind this read the official faq: github.com/google/googletest/blob/master/googletest/docs/…Lippold
D
0

In Ubuntu 22.04, you can do the following things to link gtest and gtest_main libraries:

apt install googletest
cd /usr/src/googletest
mkdir build
cd build
cmake ..
make
cmake --install .

This script will compile the static libraries libgmock.a libgmock_main.a libgtest.a libgtest_main.a. Then use cmake --install to avoid manually copying something to /usr/local/lib

Then the CFFLAGS in your Makefile should look like:

CFLAGS= -lgtest -lgtest_main -lpthread -Wall

if you use gmock, also add -lgmock -lgmock_main (and cd /usr/src/googlemock... )

Denbrook answered 26/6, 2023 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.