开发者

Are C files renamed to C++ are going to be compilable with C++ compilers after renaming?

开发者 https://www.devze.com 2022-12-23 15:33 出处:网络
C files renamed to C++ are going to be compilable with C++ compilers after renaming? So 开发者_开发技巧I have 25 C files and 1 C++ file I do not want to create Make files or anething like that. I want

C files renamed to C++ are going to be compilable with C++ compilers after renaming? So 开发者_开发技巧I have 25 C files and 1 C++ file I do not want to create Make files or anething like that. I want to turn all that C files into C++ so will simple renaming work or what shall I do?


In general, yes. You will have to worry about variables named class and such, the sizeof a character literal, and name mangling, and some other rarely encountered issues. If you're converting C99 to C++, you'll have to drop some C99 features.

With regards to name mangling, reference any C symbols by using extern "C". A common idiom is:

#ifdef __cplusplus
extern "C" {
#endif

extern int myGlobal;
extern int myFunction(void);
/* etc */

#ifdef __cplusplus
}
#endif


As usual, you don't give us enough to go on, but if you are asking about the Microsoft and /or GCC compilers they will, in the absence of explicit information, base the way they compile a file on its extension. If you rename a .cpp file to .c, it will be compiled as C source, rather than C++ (and vice versa), but this in no way means that the file will compile without errors.


Depends somwhat (maybe) on the compiler, but gcc will compile / mangle them as C++ once you name them that way.

This was the big advantage of C++, back in the day. Valid C is valid C++ (mostly), so moving to the new language was trivial.


They may or may not compile as C++ files. The languages are close, but not the same. For example, in C, a variable may be named 'new' but in C++ that is a keyword, so

int new = 12;

will compile in C, but not in C++. Same for 'class', 'public',... etc C++ keywords.

The overall goal was to make it either compile, or fail to compile, but never compile wrongly. Although I'm not sure if they 100% succeeded.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号