How to force Eclipse to use g++ instead of gcc? - c++

I already asked how to call a C++ constructor from a C file in How to call a C++ constructor from a C-File. Now when I successfully apply these methods suggested there, I receive an error
fatal error: string: No such file or directory compilation terminated
this error message points to the line: #include <string> in a header of a .cpp file.
I already found out that <string> is used by c++/g++ and <string.h> by c/gcc. Well the problem got clearer, when I checked the console output and there I can see, the (.cpp) file with the error was called by the gcc, which actually expects the <string.h> but that's not my intention - I need to compile it with the g++.
Now my question is: Can I force Eclipse to use a specific compiler? Here, for example just g++ (I heared it is capable of C-code too.) - Or even better, is there a way to chose the compiler for each directory in the workspace ?
Thanks for your advises

Answer respecting the wish of being able to specify the compiler for every subfolder:
What you are searching is probably a makefile project. That allows you to specify the toolchain, being for example the preprocessor, compiler and linker. g++ is an example for such a toolchain, as much as clang++ would be.
You can generate such a project in eclipse, writing the makefiles by hand, or use some build environment, such as CMake, which I would recommend for better portable code.
Both solutions would allow you to specify the compiler, as well as the compile flags, for every single directory of your project, if you wished so.
Writing a makefile for your existing C/C++ project can be achieved by completing the following steps:
in the folder where your source file is, right click and create a new file. New > File
name it makefile and click Finish
The new makefile should pop up in the editor and can be filled like follows:
makefile:
all: executable_name
clean:
-rm main.o executable_name[.exe on windows] executable_name
executable_name: main.o
g++ -g -o executable_name main.o
main.o: main.cpp
g++ -c -g main.cpp

Change Project's Setting can force eclipse to compile using g++:

Related

How can I compile three files together? [duplicate]

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?
list all the other cpp files after main.cpp.
ie
g++ main.cpp other.cpp etc.cpp
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
To compile separately without linking you need to add -c option:
g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?
Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
# "all" is the name of the default target, running "make" without params would use it
all: executable1
# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)
# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o
That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.
.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into myprgram.
then run your program ./myprogram
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
This will generate "myprogram"
run the program ./myprogram
that's all!!
The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)
p.s Use this method only if you don't care about makefile.
You can still use g++ directly if you want:
g++ f1.cpp f2.cpp main.cpp
where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.
As rebenvp said I used:
g++ *.cpp -o output
And then do this for output:
./output
But a better solution is to use make file. Read here to know more about make files.
Also make sure that you have added the required .h files in the .cpp files.
You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).
If you want to use #include <myheader.hpp> inside your cpp files you can use:
g++ *.cpp -I. -o out
I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.
g++ *.cpp *.h -Wall && ./a.out
It will compile and execute at the same time.
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
$g++ -g -Wall cow.cpp cow.cc -o cow.out
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!

qmake: compile single cpp file into an executable

I got a project in QT and I'd like to use qmake for compiling one additional cpp file that (into standalone executable) is not connected in any way to my application or even QT (it's very simple plain C++ program). Is there any way to do this without rebulding whole project structure? Do I need separate .pro file for every executable or is there any other way for simple compiling just one, plain C++ file?
As you may know qmake -project will make one .pro file with the name of the folder containing your whole source and header files, if you qmake this pro file then make your project you will get compiled .o file from your new cpp file even if it's not connect to your Qt project directly.
but if this file got main() function of course you will have multiple main() definitions error by compiler.
you will need to rebuild that file of course
as you know for simple compiling of only one standard plain c++ file you just
g++ source.cpp -o excutable_name.exe
for more strict compiling with two steps:
g++ -Wall -pedantic -ansi source.cpp -c compiled_file_name.o
g++ compiled_file_name -o excutable_name.exe
but if you are going to use for example a code related to Qt, you have to include Qt headers and link necessary libraries :
g++ -Wall source.cpp -c compiled_file_name.o -L qt/library/path -lQtGui -lQtCore -lQtother_necessary_libraries -I Qt/include/path -I Qtother_necessary_include_paths
To make an additional executable you can use use system() in .pro like so:
system(g++ otherapp.cpp)
Which will be built every time you call qmake. However if you want to build the additional app automatically when its source is changed, use QMAKE_EXTRA_TARGETS instead.

include class and compile with g++

Im a beginner in C++ and working with unix. So here is my question.
I`ve written few lines in the main-function, and i needed a function, that is defined in the c_lib - library.
main.cpp:
#include <iostream>
#include "c_lib.cpp"
int main()
{
return 0;
}
i want to execute it on the terminal, so i wrote
g++ -c c_lib.cpp
g++ -c main.cpp
g++ -o run c_lib.o main.o
Until here, there is no error report.
Then
./run
I get the error
error: ./run: No such file or directory
What's wrong?
Including a .cpp is not usually done, usually only headers are included. Headers usually contain the declarations that define the interface to the code in the other .cpp
Can you show us the source of c_lib? That may help.
As the source of c_lib is #included, there is no need to compile it seperately. In fact this can/will cause errors (multiple definitions being the first to come to mind). You should only need to do:
g++ -o run main.cpp
to compile your code in this case.
(When using a header (.h), you will need to compile the implementation (.cpp) seperately)
Compile with warnings turned on:
g++ -Wall -Wextra -o run main.cpp
and you will get more output if there are problems with your code.
Is the run file being output by gcc? You can test by calling ls in the terminal (or ls run to only show the executable if it is present).
If the executable is present, it could be that it isn't marked as runnable. I'll go into that if it is a problem as it is outside the general scope of the site (though still related)
First of all you should not include source file into another source. You should create a header file and put declarations there (that allows main() to call functions from c_lib.cpp or use global variables if any)
When you run g++ you have to look into it's output, if operation succeed or not. In your case it failed so executable run was not created.
Usually you do not call compiler manually but write a makefile and let make utility to call g++.

error with makefile

I want to compile my C++ files with mingw-g++ in command prompt. My C++ files have OGRE3D libraries also. How can I add these OGRE3D libaries in makefile.
For example after I compile my files in command prompt I get an error like this ; OgreEntity.h :No such file or directory
To your g++, you should give options. Some useful options are:
-I/path/to/library/include This tells the compiler to look for library
headers in this folder also
-L/path/to/library/lib This tells the compiler to look for library's lib file.
For example, let's say it's called libBerzos.a
-lLibname This tells the compiler to which library it should link. In the
example above, you would write -lBarzos
For example, let's say I have written a library myself named shSGL. I have the files in C:\shSGL
Then if I want to compile a file using it, I would compile it like this:
g++ -c -o file.o file.cpp -IC:/shSGL/include
and build the executable with
g++ -o exec file.o -LC:/shSGL/lib -lshSGL
See this Makefile for a real example.
If you want to learn more about g++ options, just search for man g++ in google and the first site would be this.

Using G++ to compile multiple .cpp and .h files

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?
list all the other cpp files after main.cpp.
ie
g++ main.cpp other.cpp etc.cpp
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
To compile separately without linking you need to add -c option:
g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?
Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
# "all" is the name of the default target, running "make" without params would use it
all: executable1
# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)
# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o
That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.
.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into myprgram.
then run your program ./myprogram
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
This will generate "myprogram"
run the program ./myprogram
that's all!!
The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)
p.s Use this method only if you don't care about makefile.
You can still use g++ directly if you want:
g++ f1.cpp f2.cpp main.cpp
where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.
As rebenvp said I used:
g++ *.cpp -o output
And then do this for output:
./output
But a better solution is to use make file. Read here to know more about make files.
Also make sure that you have added the required .h files in the .cpp files.
You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).
If you want to use #include <myheader.hpp> inside your cpp files you can use:
g++ *.cpp -I. -o out
I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.
g++ *.cpp *.h -Wall && ./a.out
It will compile and execute at the same time.
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
$g++ -g -Wall cow.cpp cow.cc -o cow.out
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!