How to run C++ program using GCC? - c++

I need to run a demo program, ba_demo.cpp, which is in a folder structure, "C:/root/demo_parent1/demo_parent2/demo.cpp", and this file uses the following code in it.
#include "root/sub1/sub2/header_file.h".
The file is at
'C:/root/sub1/sub2/header_file.h'
When I try to compile the demo program by the command
C:\root\demo_parent1\demo_parent2> gcc demo.cpp
it is not finding the header file and is throwing an error. What changes should I make to my command in order to run the demo program successfully?

First you must inform your compiler where to look for root directory by adding flag -IC:
so your command will look like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:
But that's not practical. Better change include to
#include header_file.h
and add only /root/sub1/sub2/ directory, which will look like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\
Now you have your program compiled to a.out file. You execute it by
C:\root\demo_parent1\demo_parent2>./a.out
You can change output name to demo.exe like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\ -o demo.exe
Please note that since you work on Windows under some &nix simulator the slashes in my examples might be wrong but I can't check them now. Use google to check how to handle them properly.
EDIT: Also, don't forget to take Lukas Holt's advice and use g++! It is very important to use proper compiler for proper language.

Your .cpp file is in: C:/root/demo_parent1/demo_parent2/demo.cpp
You include:
#include "root/sub1/sub2/header_file.h"
This is a relative path. You need either to specify an include directory with the -I flag, or to move header_file.h to C:/root/demo_parent1/demo_parent2/root/sub1/sub2/header_file.h.
The most simple solution would be to just #include "header_file.h" and to move it into the same directory as demo.cpp.
Then you will be able to compile it with gcc, and to execute the generated file.

Related

Cannot Compile C Program That Uses a Library (FFmpeg) with GCC Because of the Library's Include Statements

I am unable to compile a C project that uses a library called "FFmpeg" with a compiler called "GCC", and I believe it might be either because I don't quite understand how #include works or because I am using the wrong compilation process.
In a folder called Test, I have a file Test/test.c with the following contents:
#include <stdio.h>
#include <stdlib.h>
#include "FFmpeg/libavcodec/avcodec.h"
The folder FFmpeg is located at Test/FFmpeg. When I try to compile this with GCC, I receive the following error:
fatal error: libavutil/samplefmt.h: No such file or directory
The file Test/FFmpeg/libavcodec/avcodec.h has the following code in it:
#include "libavutil/samplefmt.h"
#include "libavutil/attributes.h"
#include "libavutil/avutil.h"
... //many more #include statements
Is the issue here that I need to add "FFmpeg/" to all of these include statements?
If so, is there a way to automatically do this? This library is enormous and probably has hundreds of these statements.
If not, what should I be doing instead? Should I attempt to compile the library by itself? If so, how do I then include this compiled version of the library in my program?
Notes:
The command I am using to compile is gcc -c test.c.
I have GCC installed via MinGW.
I ultimately need to be able to compile this program to both a .dll and an .so.
I apologize if any of the terminology I use here is incorrect or if my explanations are poor. I know almost nothing about compilation. Please let me know if I need to fill in more information.
When #include is used with quotation marks (e.g. #include "file path here"), it will read that file path as a relative file path.
In the case of compiling a C program using GCC, file paths are relative to the current directory. The "current directory" is the one into which you have placed your command prompt using the cd command.
In my case, I cd'd into C:/Users/User/Documents/Test, meaning that all relative file paths are relative to C:/Users/User/Documents/Test. So when my compiler read
#include "libavutil/samplefmt.h"
it basically tried to do this:
#include C:/Users/User/Documents/Test/libavutil/samplefmt.h
when I instead needed the compiler to look at …/Test/FFmpeg/libavutil/samplefmt.h.
It turns out that the solution to this is to give the compiler additional locations to which relative paths might be relative. This is done with the -I[file path here] argument when you compile.
In my case, the way I needed to use this idea was to add C:/Users/User/Documents/Test/FFmpeg as a location to which paths might be relative. Thus, I could have taken my compile command:
gcc -c test.c
And inserted this:
gcc -IC:\Users\User\Documents\Test\FFmpeg -c test.c
However, this is actually an extremely clunky solution. There is a much easier way: it turns out that these file paths you provide with the -I argument can be relative to your current directory themselves. In my case, because my current directory in the command prompt was alreadyC:/Users/User/Documents/Test, I could simply remove this portion from the above command, shortening it to this:
gcc -IFFmpeg -c test.c
And this solved my problem.

Using gcov to test a c++ program

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

Compiler option for missing include file on Linux

It's been a while since I've dealt with C/C++, so forgive me if this is a ridiculously easy to answer question - I just don't quite know how to "Google" it.
I have a file, "MyFile.h" that includes file "includedFile.h". However, the compiler cannot find the file. Please see below picture:
What I'm doing is moving the project from an old Solaris box to a Linux box. The weird thing is that it worked on the Solaris box as-is but Linux is a little confused.
The makefile that I use for the project hasn't changed either which makes me think that it may be a compiler option...
So how do I tell the compiler on Linux where that include file is, or how do I specify it in "MyFile.h?"
With gcc and clang, you specify the include path using -I:
g++ -o myprogram main.cc extra.cc -I/usr/include/boost -I/my/extra/include/files
You can specify full paths in your files, as in #include "/path/to/my/includedfile.h", but I strongly discourage this as it forces everyone who wants to compile your code to comply with that directory layout.
Also relevant: Read the following link for the difference between #include <file> and #include "file" in gcc: http://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html
Assuming you are using g++, you pass a path with the -I flag.
g++ ..... -I<a path to your includes> -I<another path to includes>

Creating shared library containing other libraries

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.

Vim code completion doesn't work after including a standard header

I have to develop my project in text-mode debian linux. I'm using Vim and I installed the clang_completion plugin on it. I made .clang_completion file in root of my project :
-I.
-I/usr/include
-I/usr/include/c++/4.6
When I write a program like below, the completion works fine.
//#include <stdio.h>
int main()
{
struct A
{
int x, y;
};
A a;
a. // After putting dot, the suggestion popup appears
return 0;
}
However, after removing the comment of first line, it doesn't work! How can I overcome this issue?
I found the easiest way to get clang_complete to work is to use the provided cc_args.py file.
when compiling a project use clang_complete/bin/cc_args.py instead of gcc/g++
This will generate the correct .clang_complete file with all libraries and dependencies.
Provided the clang_complete source directory in your home folder.
Example Makefile:
CXX=$(HOME)/clang_complete/bin/cc_args.py g++
all:
$(CXX) main.cpp
I've successfully used the clang_complete plugin in the past (now I just use cscope and ctags, which I consider enough).
Including external headers worked fine in my configuration, but, as the clang complete plugin page specifies, the file in which to put include paths (or any other flag you may want to pass to the clang compiler), must be named .clang_complete and not .clang_completion.
Also, I used to put the options on a single line, just as I was going to pass the plain content of the .clang_complete file as a command line option (don't know if separating lines with \ will work).
Hope this helps.