Is there anything special I need to do to use the C code in my C ++ program?

Note. I am using g ++ version 4.3.4 to compile C ++ code.

Until then, when I wanted to use elements of the C style language in my code, it seems that I can just include the C material mixed with and near my C ++.

I know that C ++ is basically backward compatible with C ... so I think my questions are as follows:

What parts of C are not compatible with C ++?

Will professional programmers laugh at me if I continue to naively embed C material in my C ++ code?

What is the correct way to have C and C ++ code in the same .cpp file?

Can I continue to use g ++ to compile my hybrid code?

For this question, I'm mostly interested in a solution that deals with one .cpp file and one g ++ command to compile it. At the moment, I still don't need to bind things together.

+4
source share
6 answers

After choosing a few questions:

"What is the correct way to have C and C ++ code in the same .cpp file?" "Can I continue to use g ++ to compile my hybrid code?"

If you want to mix C ++ C-style in the same file as regular C ++, just go ahead and do it. You can trust the compiler to select any problems - they will be minimal and will not affect the structure. According to him, you are not interested in getting a C-linkage for yourself, therefore, even if the C-code is in your own file, compile it as C ++. In fact, this is often done as a way to migrate from C to C ++.

If you take this approach, your code will not be truly hybrid C / C ++. This is C ++ with some code using C-style procedural idioms. C ++ is fully intended to support this.

"Will professional programmers laugh at me if I continue to naively embed C material in my C ++ code?"

It depends on where you use it and why. Well-structured C code is good code. Sometimes C + is much better than C, with special problems. Think Before You Use C-Style Dynamic Memory Management. You deserve to be laughed at if you use raw malloc()/free() and make a mistake.

I suggest that if you start a similar approach, you can take the time to look back and think it would be better to use C ++ idioms for procedural C.

+3
source

The big problem with binding C and C ++ code is that the C ++ compiler needs to know that it associates with functions that use C calling conventions, and not with C ++ calling conventions. For this purpose, you often have to collapse your C header files like this:

 #ifdef __cplusplus extern "C" { #endif ... C function declarations here ... #ifdef __cplusplus } #endif 

See C ++ Frequently Asked Questions for details.

+3
source

C ++ is almost a superset of C. Therefore, almost any function that you can use in C is valid in C ++ (but not vice versa).

People may laugh at you for coding some things, like in C, but ignore them. I don’t think it is naive, but it can make me naive. I can not tell.

There is no [real] way to separate C and C ++ code in the same file. It just happens to each other, because when you compile it as C ++, it is not C anymore. So β€œC with C ++” is really not a way to think about it.

The only difference between the thing in C that will not allow the compilation of code like C ++ that I know of (besides C ++ with a lot of keywords) is the problem with void* s. In C, they will be implicitly applied to any type of pointer, but in C ++ you must have an explicit cast to cast them to another type of pointer. There may be others, but I do not know them.

Oh, also, C ++ does not support "default int". I don't know if it will still be valid C these days, but if you leave the variable type or the return type of the function, the compiler will just use int . C ++ does not do this.

+2
source

Most good C methods are for compiling C ++. A couple of extra pointers and renamed identifiers will do for legal C ++.

In style, however, C-style code is considered terrible C ++. The fact that you can write C-style code in C ++ should be used if and only if you cannot afford to write it in C ++ to start with it, that is, if it is outdated code.

Will professional programmers laugh at me if I continue to naively paste C into your C ++ code?

I guess, yes. The C-style coding is known in C ++ as "terribly dangerous", just for starters. This code is written only by people who do not know how to use C ++. That way, I don't mean to do very low-level things like bit-twiddling or binary reinterpretation, but things like casting or manual resource management, and that will make you laugh.

+2
source

This is too broad a question; you have to break it into several. There is a way to link C and C ++ object files; C ++ is backward compatible with C, and you can use the C ++ compiler to compile C code.

But consider this code:

 int main(void) { int class = 0; int private = 1; return private-class; } 

This is valid C code and obviously will not compile on any C ++ compiler if it is compiled as C ++ code. Just an example.

+1
source

Well, the obvious key difference is that C ++ is object oriented in design, while C is not. However, you should not have terrifying shutdown problems compiling C code with g ++ unless you come across one of the nasty bugs that float around. You can find many good links for them, and I admit that my answer is far from exhaustive.

It is true that you cannot implicitly drop from void* in C ++; it will not compile, whereas you will see it quite often in C.

You must also explicitly declare functions in C ++ before using them, where you do not need to in C. (This is a good form for this, but not all C programmers).

http://www.cprogramming.com/tutorial/c-vs-c++.html Now that I have everything in order, this is a good recommendation; I often use it.

0
source

All Articles