-1

This might be a stupid question, but I'm not 100% sure and can't find a good answer via googling, so I thought I should ask it.

I'm building a few C libraries on a new compiler that will be linked with C++ code. I'm compiling the libraries from scratch because I'm moving up compiler versions from gcc4.x to gcc5.x, which requires me to recompile all of my C++ libraries with -std=gnu++14 (the language standard I'm targeting). My question is, do I need to add -std=gnu++14 to my CFLAG values when compiling to C libraries? I don't think I do, but want to confirm that I won't run into major issues down the road.

Thanks.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
user797963
  • 2,907
  • 9
  • 47
  • 88

2 Answers2

1

No. When you are compiling C code with a C compiler, you can specify the version of the C language you are using, but you cannot specify the C++ language version. It would not make sense to specify a C++ language version because there is no sensible effect that such a setting could have.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

Since C++ compiler will mangle symbol names, you'll want to surround C code with a extern C { block. The common idiom is like so.

#ifdef __cplusplus
extern "C" {
#endif

/* My C symbols, functions, etc */

#ifdef __cplusplus
}
#endif
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • is this really necessary? can he just make the code in *.c file? – Nick Sep 27 '16 at 19:25
  • 1
    @Nick: It would be good to read about the difference between headers and libraries (i.e. object files). – too honest for this site Sep 27 '16 at 19:27
  • 2
    @Nick it's *absolutely necessary* the file extension does not really matter, but gcc will try to compile based on it, but you can override that. – Ryan Sep 27 '16 at 19:31
  • 4
    I've gotten in this fight before.. my personal preference is to not include any `C++`-compatibility stuff in my `C` code. Makes more sense to me to have a distinct separation between the two and keep them modularized. If `C++` wants to call `C` code, then it can go to the `extern "C" {...}` trouble. But I suppose the end results are the same so who cares. – yano Sep 27 '16 at 19:32
  • @self _"it's absolutely necessary the file extension does not really matter"_ This statement isn't really consistent with your answer then. You probably should add this in 1st place to it. – πάντα ῥεῖ Sep 27 '16 at 19:54
  • This is tangentially related, but you did not answer the user's question. – David Grayson Sep 27 '16 at 20:01
  • yano: Your preference seems really bad to me. It is very valuable to developers if they can just take working C code, paste it into a C++ program, and have it keep working. That is only possible because header files for C libraries include bits of code like what was posted in this answer. You seem to be valuing tidiness of header files over the user experience. – David Grayson Sep 27 '16 at 21:10