I am using gcov for the first time to analyze my program (C++)
The program consists of three classes and I have built the project using Code::Blocks.
When I am invoking the program using the following command:
C:\Users\XXX\Documents\Test\TreeObjModel\src>gcc
-fprofile-arcs -ftest-coverage Tree.cpp
I receive the following error:
Tree.cpp:1:18: fatal error: Tree.h: No such file or directory
compilation terminated
While the cpp files are in the directory "C:\Users\XXX\Documents\Test\TreeObjModel\src\" , the header files are in directory "C:\Users\XXX\Documents\Test\TreeObjModel\include\"
Do we need to have both the code and header files in the same directory?
Thanks in advance.
You should use the -I flag to specify where your header files are.
Judging from your example, you should add -I../include
You have at least two options to instruct the compiler where to find the header files (includes).
-Ipath_to_includes as parameter for gcc compiler. E.g. -I../include
When including in your program, specify the directory. E.g. #include "../include/foo.h"
My strategy would be to just compile my project successfully and only then try to use some other stuff, like flags for code coverage. I say this because your error does not have anything to do with gcov, and trying to instrument your program to get code coverage before your program even compiles, makes things more complicated for you. One step at a time ;)
Related
I'm using g++ on linux and writing multiple cpp programs. For ex. I have ex1.cpp, ex2.cpp, bot.cpp.
All these cpp programs are inside the same folder. I want to use make to compile individual programs as in "make ex1" should compile ex1.cpp and not the other cpp files. "make bot" should only compile bot.cpp
The compilation command I intend to execute is :
g++ -o bot bot.cpp
I don't want to write target and dependency in MakeFile for every cpp program I create in the folder and I don't want to compile all cpp's at one go using *.cpp.
Is it possible first of all to achieve this ? Is yes please suggest a solution
A simple solution to your question :
Just run make ex1 in the command line and it will compile using the command g++ ex1.cpp -o ex1.
If you want to dynamically include libraries/shared objects or add additional flags then you should create a make file or follow the procedure suggested by Lukasz (Too complicated though).
You don't need to write a makefile. make ex1 should already do what you want.
I tried to search this question online, but it seems that I can't find a good solution for my problem. Well, I'm trying to use gdb to debug my c++ program. And the program is made up of a simple main.cpp and a model.h. And the compiling command is
g++ -Wall -g -c main.cpp
g++ -Wall -g main.o -o OUTPUT
As almost all the algorithm is stored in model.h, I need to debug that header file rather than the cpp file. However, whenever I tried to place a break point on the header like
tbreak model.h:163
gdb always give me a message that"No source file named TNFmodel.h".
In another question breakpoints in GDB, I saw a solution by adding the folder that containing the header into the library by "dir". But my header file is already in source folder, and after trying
dir ./
The problem maintains.
So anybody know what's wrong? How to use gdb to debug a header file?
As suggested by https://stackoverflow.com/users/760746/nobody, one way to make sure the header to be in the sources is to veryfy it by checking
info sources
After ensuring the header itself be in the sources(in my case, the problem is that the case of a letter in the header name was mixed up, and somehow it went through the compiling on my mac book), inserting breakpoint in lines of a header file works just fine.
Try to use break with your class/method name like this:
break class::method
What I've found is that is that the file names are sometimes shortened. Using info sources I was able to find the shortened name that GCC used. When I set the breakpoint using the shortened file name, GDB correctly set the breakpoint.
For example the file CommonLibrary\headers\Endian.h was changed to COM~2\headers\Endian.h
This on Windows 10, running mingw-64.
I'm using some methods from the c++ AlgLib library. In other libraries I've used there were some instructions for installation, after which I could include .h files and compile with -l (e.g. using the GMP-library and compiling with -lgmp). However with alglib the only thing I've been able to get running is using a makefile and compiling all needed .cpp files - every time I compile my program. Here is an example of a makefile:
all:
g++ name.cpp ap.cpp integration.cpp interpolation.cpp alglibinternal.cpp linalg.cpp
alglibmisc.cpp specialfunctions.cpp solvers.cpp optimization.cpp -o name
As compiling all these files every time is relatively time consuming, is there a way to avoid it?
You can use the make feature.
Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.
View the page: http://www.gnu.org/software/make/ to use this tool.
i've written a class from that i want to create a shared library. But this class uses other libraries. The call that i use to generate the library is of the form
g++ -fpic -c [necessary *.cpp files] [necessary includes]
Unfortunately this call leads to an error, namely iostream.h: No such file or directory.
But when i build a testfile using the library code it compiles and works properly.
Where might be the problem?
Thanks for your help.
Firstly, I'm pretty sure that you should be using iostream rather than iostream.h so you could try changing that in your source code. E.g.
#include <iostream>
Secondly, you need to check the include directives on the compilation line, as what you're getting is a compilation error indicating that the preprocessor can't find this file. As you haven't listed either your failed or successful commands here, I can't add much more than that.
I use gcc (running as g++) and GNU make.
I use gcc to precompile a header file precompiled.h, creating precompiled.h.gch; the following line in a Makefile does it:
# MYCCFLAGS is a list of command-line parameters, e.g. -g -O2 -DNDEBUG
precompiled.h.gch: precompiled.h
g++ $(MYCCFLAGS) -c $< -o $#
All was well until i had to run g++ with different command-line parameters.
In this case, even though precompiled.h.gch exists, it cannot be used, and the compilation will be much slower.
In the gcc documentation i have read that to handle this situation,
i have to make a directory called precompiled.h.gch and put
the precompiled header files there,
one file for each set of g++ command-line parameters.
So now i wonder how i should change my Makefile to tell g++ to create
the gch-files this way.
Maybe i can run g++ just to test whether it can use any existing file
in the precompiled.h.gch directory,
and if not, generate a new precompiled header with a unique file name.
Does gcc have support for doing such a test?
Maybe i can implement what i want in another way?
It seems weird to answer my own question; anyway, here goes.
To detect whether a suitable precompiled header file exists, i add a deliberate error to my header file:
// precompiled.h
#include <iostream>
#include <vector>
...
#error Precompiled header file not found
This works because if gcc finds a precompiled header, it will not read the .h file, and will not encounter the error.
To "compile" such a file, i remove the error first, placing the result in a temporary file:
grep -v '#error' precompiled.h > precompiled.h.h
g++ -c -x c++ $(MYCCFLAGS) precompiled.h.h -o MORE_HACKERY
Here MORE_HACKERY is not just a plain file name, but contains some code to make a file with unique name (mktemp). It was omitted for clarity.
There is a simpler way than introducing an #error in precompiled.h: never create this file at all. Neither G++ nor Visual C++ (at least up to 2005) expect the "real" file to be there, if a precompiled version is around (and if they get the necessary compilation flags).
Let's say the list of #includes that we want to precompile is called "to_be_precompiled.cpp". The filename extension doesn't matter much, but I don't like to call this a .h file, since it has to be used in a way different from genuine header files, and it's easier in Visual C++ if this is a .cpp. Then pick a different name to refer to it throughout the code, let's say "precompiled_stuff". Again, I I don't like to call this a .h file, because it's not a file at all, it's a name to refer to precompiled data.
Then in all other source files, the statement #include "precompiled_stuff" is not a genuine include, but simply loads precompiled data. It's up to you to prepare the precompiled data.
For g++, you need a build rule to create "precompiled_stuff.gch" from a source file whose name doesn't matter to the compiler (but would be "to_be_precompiled.cpp" here).
In Visual C++, the string "precompiled_stuff" equals the value of the /Yu flag and the precompiled data loaded comes from a .pch file with an unrelated name, that you also created from an unrelated source file (again "to_be_precompiled.cpp" here).
Only when building with a compiler without precompiled header support, a build rule needs to generate an actual file called "precompiled_stuff", preferably in the build directory away from the real source files. "precompiled_stuff" is either a copy of "to_be_precompiled.cpp", a hard or symbolic link, or a small file containing #include "to_be_precompiled.cpp".
In other words, you take the viewpoint that every compiler supports precompilation, but it's just a dumb copy for some compilers.