cannot find -lgtest when setting up Google Test - c++

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.

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.

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).

Related

Building a C/C++ program for Windows/MacOS/Linux with dependencies

I need to build a program on Windows, MacOS and Linux which relies on multiples dependencies. I tried to limit them as much as I could but I should ended up using WxWidgets, libcurl, openssl and libarchive.
I choose those dependencies because I must be able to:
compute a md5 hash (openssl)
decompress a zip archive (libarchive)
provide a GUI (WxWidgets)
use HTTPS (libcurl)
I am aware that this might be an overkill to import 5 libraries to only do those 5 things but I do not know any better solution yet. As I am writing this, I'm only using libcurl and openssl and building for Windows, MacOS and Linux is already very difficult for me.
I would like to distribute a standalone executable for each platform in addition to the source code, to build my project, I use the following Makefile :
CXX = g++
NAME = foo
SRC = $(wildcard src/*.cpp)
OBJ = $(SRC:.cpp=.o)
CXXFLAGS = -I./inc -std=c++11 -Wall -Wextra -Werror -g
all : $(NAME)
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ -lcurl -lssl -lcrypto
clean :
rm -rf $(SRC:.cpp=.o)
fclean : clean
rm -rf $(NAME) test
re : fclean all
reclean : fclean all clean
.PHONY : all clean fclean re reclean
This worked great(even on Windows thanks to MSYS2 and MINGW) until I tried to statically linked each library to distribute a standalone binary :
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ -lcurl -lssl -lcrypto
becamed
$(NAME) : $(OBJ)
$(CXX) $(CXXFLAGS) -o $# $^ libcurl.a libssl.a libcrypto.a
On Linux(Ubuntu 22.04), I downloaded curl and openssl source codes and built them using -static flags, copied the *.a inside my project's directory and I was done.
Now I'm trying to build curl and openssl on Windows with MSYS2, most of my attempts failed with error messages and took forever(> 30 minutes) to complete the build process. After struggling a little too much time, I finally managed to compile openssl by adding -lcrypto -lcrypt32..?
I do not think I will be able to finish this project with this "building process" as I still need to figure it out how build and link libarchive and WxWidgets. Is there anything I am missed out regarding the building process ? Should I pick others libraries ? Or It is just how it is ?
Thanks for your help.

dyld: Library not loaded: #rpath/libopenblas.dylib

When I make my file, I have this error:
dyld: Library not loaded: #rpath/libopenblas.dylib Referenced
from: /Users/danyunhe2/reinf_learning2/cpp_original/./navig_test
Reason: image not found Abort trap: 6
I tried ln -sf <original path> /usr/local/lib but it didn't work.
From brew info openblas I got:
openblas: stable 0.3.5 (bottled), HEAD [keg-only]
Optimized BLAS library
https://www.openblas.net/
/usr/local/Cellar/openblas/0.3.5 (22 files, 120.7MB)
Poured from bottle on 2019-02-18 at 01:27:14
From: https://github.com/Homebrew/homebrew- core/blob/master/Formula/openblas.rb
==> Dependencies
Required: gcc ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
openblas is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BLAS and LAPACK in the Accelerate framework.
For compilers to find openblas you may need to set:
export LDFLAGS="-L/usr/local/opt/openblas/lib"
export CPPFLAGS="-I/usr/local/opt/openblas/include"
It told me to set compiler with LDFLAGS and CPPFLAGS. I tried but it didn't work. Anyone know how to deal with this?
I have my config.mk as:
# C++ compiler
cxx=g++-7 -fopenmp
# Compilation flags
cflags=-Wall -ansi -pedantic -O3
# BLAS/LAPACK flags for linear algebra
lp_lflags=-framework Accelerate
# FFTW flags (installed via Homebrew)
fftw_iflags=
fftw_lflags=-lfftw3
# libpng flags (installed via Homebrew)
png_iflags=
png_lflags=-lpng
and Makefile:
# Load the common configuration file
include config.mk
iflags=`gsl-config --cflags`
lflags=`gsl-config --libs`
objs=navigate.o reinf_learn.o common.o
src=$(patsubst %.o,%.cc,$(objs))
execs=navig_test
all:
$(MAKE) executables
executables: $(execs)
depend: $(src)
$(cxx) $(iflags) -MM $(src) >Makefile.dep
-include Makefile.dep
navig_test: navig_test.cc $(objs)
$(cxx) $(cflags) $(iflags) -o $# $^ $(lflags)
%.o: %.cc
$(cxx) $(cflags) $(iflags) -c $<
clean:
rm -f $(execs) $(objs)
.PHONY: clean all executables depend
On OSX it's DYLD_LIBRARY_PATH, which you need to specify at runtime like so:
export DYLD_LIBRARY_PATH=/usr/local/opt/openblas/lib
However, please appreaciate brew's warning regarding the Accelerate framework. It is way faster with many of the BLAS operations on all sorts of levels. You will just make programs run slower.

How to link google test lib (libgtest.a) depending on architecture (ARMEL or i386)?

I use Google Test in my C++ project. I build the target either for my i386 virtual machine or for the real ARMEL target. What I would like to do is have my Makefile link in the correct google test .a file for the right architecture, depending on the host system. E.g. if I am running the test target from my i386 VM, I'd like to link in the i386 built libgtest.a, and ditto when building on my ARMEL system. How do I do that? I do not cross compile the ARM build, I build it on the ARM target hardware itself. I don't really want to build libtest.a from scratch as it's pretty slow to build.
My Makefile is:
CC = g++-4.7
CXX := $(CC)
SVNDEV := -D'SVN_REV="$(shell svnversion -n .)"'
CFLAGS += -Os -Wall -Wno-write-strings $(SVNDEV) -Iinclude -I/usr/include/c++/4.7 -Llib
CXXFLAGS += --std=c++11 -D_GLIBCXX_USE_NANOSLEEP $(CFLAGS)
TARGET = connectionmanager
LIBS = -lzmq -ljansson -lpthread
objects = systeminterface.o linuxsysteminterface.o connectionmanagerutils.o connectionmanagerexception.o logger.o jsoninterface.o configuration.o diagnosticslogger.o serial.o \
modemstrategy.o lisac200modemstrategy.o maestrom1003gmodemstrategy.o
test_objects = mocksysteminterface.o mockmodemstrategy.o test/connectionmanagertest.o test/connectionmanagerutiltest.o test/jsoninterfacetest.o test/gtest_main.o
$(TARGET): connectionmanager.o $(objects) $(LIBS)
.PHONY: clean all test
all: $(TARGET)
cmmain.o: connectionmanager.cpp
$(CXX) -DUNIT_TESTING $(CPPFLAGS) $(CXXFLAGS) -c connectionmanager.cpp -o cmmain.o
connectionmanagertest : cmmain.o $(objects) $(test_objects) lib/libgtest.a $(LIBS)
$(CXX) -DUNIT_TESTING $(CPPFLAGS) $(CXXFLAGS) $(LIBS) lib/libgtest.a $^ -o $#
test: connectionmanagertest
./connectionmanagertest
clean:
rm -f $(TARGET)
rm -f $(objects)
rm -f $(test_objects)
rm -f connectionmanager.o
rm -f cmmain.o
rm -f $(test_objects)
Please, have a look here. As well your question looks like a duplicate of this. As a suggestion - you might be interested in CMake. Another good point is to parse /proc/cpuinfo for Linux, or use a sysctl call if you are on FreeBSD. Anyway the source of primary system information must be extracted either by existing utility that will return result for later processing in Makefile or using standard system tools. As a source of ideas I would also recommend to download any opensource package and look into the Makefile generators.

Error on compiling a libmicrohttpd program. (MHD_RESPMEM_PERSISTENT)

I've looked all over the internet for this issue and doesnt appear to be a recurrent error.
The problem comes when I try to compile (I'm working on Ubuntu 12.04 distro) with g++ the main file that includes the lib, and it gets all the variables and functions except the MHD_RESPMEM_PERSISTENT variable on:
response = MHD_create_response_from_buffer(strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT);
I already checked the linkers and everything to compile with -lmicrohttpd.
My Makefile looks like this:
CC = g++ -std=c++11
INCLUDE = -I. -I/opt/local/include
CFLAGS = -g -Wall $(INCLUDE)
#CFLAGS = -g -Wall $(INCLUDE) -coverage
LDFLAGS = -L/opt/local/lib -lmicrohttpd
all: $(TARGET)
$(OBJECTS): %.o: %.cc
$(CC) $(CFLAGS) -c $< -o $#
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
I didnt attach the linker fot $Objects, but its ok.
Actually, its very weird problem, because when i installed the packages (libmicrohttpd-dev and libmicrohttpd-dbg) on other PC's it worked out ok, but in some cases this issue comes up. The only solution that I came with was to re-install the OS again and start over, because everything was unefective.
It worked before on this very same computer, with the same OS, but since i had to format it, i have no idea to correct this issue.
Any ideas?.
MHD_RESPMEM_PERSISTENT as been added in libmicrohttpd 0.9.x, so version 0.4.6-1 is too old.
With Ubuntu, I don't know exactly how you can upgrade, but as libmicrohttpd as very few dependencies, maybe just add another ubuntu repo, or download a .deb and install it manually with dependencies.
Or apt-get remove the installed one, and manually install the latest version from gnu.org

Ubuntu 12.10 - Cannot find -ltcl when I compile my C++ program

I am working on a C++ project and I need to use libtcl.
I am running Ubuntu 12.10 32bits and there is a problem when I try to compile my files :
g++ -o executable executable.o -L/usr/share/tcltk -lncurses -ltcl
/usr/bin/ld: cannot find -ltcl
libncurses is found but not libtcl...
Do you have any idea?
I have seen that libtcl8.4.so.0 libtcl8.5.so.0 exist in /usr/lib
The makefile that I am using looks like this :
CC = g++
CFLAGS = -g
LDFLAGS =
EXEC = executable
LIB = -L/usr/share/tcltk -lncurses -ltcl
all: executable
executable: executable.o
$(CC) $(LDFLAGS) -o $(EXEC) executable.o $(LIB)
executable.o: executable.cpp
$(CC) $(CFLAGS) -c executable.cpp
clean:
rm -f executable executable.o
Thanks
(Answered in a comment. See Question with no answers, but issue solved in the comments (or extended in chat) )
#soon wrote:
just create symlink to the your library like so #ln -s /usr/lib/libtcl8.5.so.0 /usr/lib/libtcl.so