3

I have ported a project to an arm cortex M7 chip, and am mucking around with makefiles for the first time, im using the gnu-gcc compiler collection.

Is it advisable to compile "c" code with the gcc driver and, compile the "c++" (app) code with the g++ driver, and then link. The c code is all low level (header files)register access addresses etc and contains no functions (yet) or attached source files.

Or can i compile all with the g++ compiler if the header files can be modified to compile with g++ if needed.

I have it set so gcc is compiling the c files, and g++ is compiling c++ and linking.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
joe blogs
  • 142
  • 4
  • 16
  • 1
    Compile C code with the C compiler — that's `gcc`. Compile C++ code with the C++ compiler — that's `g++`. If the program includes any C++ code, link with the C++ compiler (and make sure the `main()` program is compiled as C++ code). If it is all C, link with the C compiler. If the code actually manages to be bilingual, I suggest treating it as C++, but there isn't a hard and fast rule about that. – Jonathan Leffler Apr 17 '18 at 08:48
  • 2
    It's not rocket science: compile C code with a C compiler and C++ code with a C++ compiler. – Lundin Apr 17 '18 at 08:48
  • maybe i should have said, the only code in the header files is structs of pointers and macros – joe blogs Apr 17 '18 at 08:57
  • @joeblogs: That the contents of the headers are only structures, pointer and macros makes no difference that I can see. Compile C code with the C compiler; compile C++ code with the C++ compiler; if there's any C++ code in the system, link with the C++ compiler; only if there is no C++ code should you link with the C compiler. – Jonathan Leffler Apr 17 '18 at 08:59
  • Possible duplicate: https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc – Clifford Apr 17 '18 at 09:02

1 Answers1

5

The only differences between gcc and g++ are that:

  • when the driver is used to invoke the linker, g++ causes libstdc++ to be linked as part of "stdlibs", while gcc will link only libc.
  • g++ will compile .c, .h and .i files as C++ unless the -x option is specified.

Both drivers will compile C or C++ depending on either the filename extension, or command-line switches. If you invoke the compiler-driver for compilation only and invoke the linker (ld) directly, using gcc or g++ -x, it makes no difference which you use.

Equally, if you invoke the gcc driver for C++ code and explicitly link stdlibc++ it also makes no difference - so long as your crt0.o is not C-only - a C++ runtime start-up must invoke global static constructors before main()) - this is likely to already be the case.

The definitive word from the documentation:

3.3 Compiling C++ Programs

C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats ‘.c’, ‘.h’ and ‘.i’ files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a ‘.h’ extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.

When you compile C++ programs, you may specify many of the same command-line options that you use for compiling programs in any language; or command-line options meaningful for C and related languages; or options that are meaningful only for C++ programs. See Options Controlling C Dialect, for explanations of options for languages related to C. See Options Controlling C++ Dialect, for explanations of options that are meaningful only for C++ programs.

If you want to use just one, I suggest you use gcc and separately invoke the linker or explicitly link -libstdc++. That way the compilation mode will be dependent on the filename extension. Using g++ -x to compile C code is just going to cause confusion.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • crt0.o is object code generated from crt0.s - it is the C runtime start-up code for your platform. It is linked by default (I recall), but can be customised for your platform. Mostly however your platform specific toolchain's default crt0.o will suffice at least for the C/C++ environment - hardware initialisation is likely to be elsewhere. – Clifford Apr 17 '18 at 09:07
  • 1
    thats the ticket. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats ‘.c’, ‘.h’ and ‘.i’ files as C++ source files instead of C source files unless -x is used. This program is also thats what i was after, i had read it before but couldnt quite remember.useful when precompiling a C header file with a ‘.h’ extension for use in C++ compilations. On many systems, g++ is also installed with the name c++. – joe blogs Apr 17 '18 at 09:29
  • @joeblogs : Yes - I was modifying the answer to make that clear while you must have been typing - sorry. – Clifford Apr 17 '18 at 09:31
  • @sanjivgupta : Yes - https://stackoverflow.com/questions/20047218/what-is-the-difference-clang-clang-std-c11 – Clifford Jan 13 '20 at 18:12