0

I have a C++ project in which I need to use some external C sources. I need to compile them with the same Tool as the project uses: GCC C++ compiler.

I Visual Studio there is a setting to set this for each C source: C/C++ -> Advanced -> Compile as C++ Code (/TP).

Can I do this with Eclipse CDT ?

7
  • Yes, just create c++ project or set g++ compiler in build project properties Commented Nov 5, 2015 at 9:03
  • You cannot generally compile C code with a C++ compiler as C is not a subset of C++. For example, int *p = malloc(sizeof *p); is legal C, but not C++. Commented Nov 5, 2015 at 9:03
  • @BaummitAugen i think you are wrong. You can use malloc or any other c function in c++ and compile it with g++. Commented Nov 5, 2015 at 9:07
  • 1
    @clsbartek read closer, and perhaps try it Commented Nov 5, 2015 at 9:09
  • @clsbartek Of course you can use malloc() in both languages, but that wasn't Baum's point. The syntax is legal in one language, but not in the other. Commented Nov 5, 2015 at 9:10

1 Answer 1

3

In project options:

  1. You could add -x c++ to the compiler flags:

    enter image description here

  2. Alternatively set the tool to g++ instead of gcc

    enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank very much for this two solutions. There is also the possibility to get rid of this kind of error in the mentioned C code ? #if UNIX_PLATFORM && !defined(__cplusplus) #error This file needs to be compiled as C++ on Unix #endif <-- this still gives me an error for __cplusplus not defined
As you could see in my live stream, the #error doesn't trigger with -x c++. It does without. Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.