I'm afraid that it is a basic problem, I might have found the answer with other IDEs but not with CodeBlocks =(
I try to use GoogleTest. I downloaded the project, built it with cmake -G "CodeBlocks - Unix Makefiles" (I'm on Ubuntu)
That gave me the libgtest.a and libgtest_main.a files that I transfered in /usr/lib
And I think my lacks are on the last step : linking GoogleTest and CodeBlocks. Some tutorials told to add the "-lgtest" linker option, I tried many things but it's all the same : when I try to compile my #include "gtest/gtest.h" returns the fatal error "No such file or directory".
Could you help my poor soul ?
Don't put files that you have built yourself under /usr/...
unless you put them under /usr/local/...
. The rest of /usr/...
is reserved by your Linux distro and should be exclusively populated by your package manager. If you mess about in it behind the package manager's back,
you risk breaking your packages and getting into any amount of pain.
First remove libgtest.a
and libgtest_main.a
from /usr/lib
.
You've downloaded googletest
and built it in some directory by first
running CMake
in that directory and then running make
.
Having done that, in the same directory where you ran make
, run:
sudo make install
This will install all the gmock
/ gtest
libraries in /usr/local/lib
.
It will install the gmock
header files in /usr/local/include/gmock
and install
the gtest
header files in /usr/local/include/gtest
. Go there and check.
/usr/local/...
belongs to you and whatever you do there will not interfere
with the package manager.
Next, to use googletest
in your unit-testing program, you might need to do the following
6 things:
1: #include <gtest/gtest.h>
where applicable in your source files. (Note:
<gtest/gtest.h>
, and not anything different).
2: Tell the compiler what directory contains gtest/gtest.h
, unless
it's one of the compiler's default search directories. To do that, you would
pass the option -I/usr/local/include
to g++
for compiling the source files. But you don't need to
do that, because /usr/local/include
is one of the compiler's default search
directories.
3: Tell the linker that you want to link libgtest.a
. To do that that, you
pass the option -lgtest
to g++
for linking the program.
4: Tell the linker what directory contains libgest.a
, unless
it's one of the linker's default search directories. To do that, you would
pass the option -L/usr/local/lib
to g++
for linking the program. But you don't need to
do that, because /usr/local/lib
is one of the linker's default search
directories.
5: Tell the compiler to generate thread-safe code. (Why? Because by default,
libgest
is a multi-threaded library. You can build it single-threaded if you
want, but I assume you didn't.) To do that, you pass the option -pthread
to g++
for compiling source files.
6: Tell the linker to link a multi-threading executable. To do that, you pass the option
-pthread
to g++
for linking the program. (Yes, -pthread
for compiling and
-pthread
again for linking).
So in practice you just need to do 1, 3, 5 and 6.
1 needs no futher explanation. The rest require you to set up your Code::Blocks project
Build options correctly, so that they generate the correct g++ ...
commands for compiling and
linking.
It looks as if you've already got 3 right: In Build options... -> Linker settings ->
Other linker options, add the line: -lgtest
For 5, in Build options... -> Compiler settings -> Other compiler options, add the line: -pthread
For 6, in Build options... -> Linker settings -> Other linker options, add another line: -pthread
,
after the one with -lgtest
.
That's all. When you've saved those settings you can build your project. If there are errors, youi can look at the
build log under the Build log tab (not the Build messages tab) to see exactly what g++ ...
commands
got executed for compiling and linking, and if your settings produced the correct commandline options for g++
.