user3085931
user3085931

Reputation: 1803

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

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?

Upvotes: 0

Views: 4193

Answers (2)

Howard Lou
Howard Lou

Reputation: 79

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

Upvotes: 0

Theolodis
Theolodis

Reputation: 5102

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:

  1. in the folder where your source file is, right click and create a new file. New > File
  2. name it makefile and click Finish
  3. 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
    

Upvotes: 1

Related Questions