using c class and header file in C++ application - c++

Posting a basic question about using C style .c and .h class in a C++ application.
I have a library which is meant for C but based on the documentation i can also use for C++.
Should i need to rename the two files as .cpp and .hpp before i start including them in my project ?
I tried to refer existing thread but it talks about other way crom cpp to c.
How to convert C++ Code to C

No you don't. The .h extension is shared.
The implementation file extension depends on the compiler/IDE. For example, MSVS will compile .c files as C source code, and .cpp files as C++. That means you'll have to use
extern "C"
in the header if you use the C functionality in the C++ part of the project.
AFAIK you can compile .c files with g++ as C++, so the extension change is not necessary. Or you can compile them with gcc and use extern "C" again.

Related

Can I use symbols defined in a .c file in a .cpp file?

I prefer coding in C but I'm using DirectX which is a massive pain to use without C++ so all my files are .c except the ones that deal with DirectX. However whenever I try to link a C file with external symbols that are in a C++ file or vice versa I get unresolved external symbol linker error. Is it possible to link a project that uses .cpp and .c and have the symbols be recognized?
Can I use symbols defined in a .c file in a .cpp file?
Yes, you can, with the requirement that the names mustn't be reserved in C++ (such as keywords that don't exist in C etc.).
In order to use the C names in C++, they must be declared with C language linkage using a extern "C" declaration.
I am assuming you are using a Microsoft environment like Visual Studio. If you open up the project properties, under Advanced, one of the options is Compile As. There are 3 options
Default - .c compiles as C, .cpp compiles as C++
/TC - both .c and .cpp compile as C
/TP - both .c and .cpp compile as C++
If you wish to use extern "C", which is the option that most people go for, just leave it as default. If you do not wish to use extern "C", then try option 3 - compile everything as C++.
Option 3 assumes you are no using c99 specifics like dynamic array allocation on the stack.

difference between different c++ library method?

As I know, we can write our c++ library in .h file,.C file and .c &.h file?
can someone describe difference between these 3 library method?
First, a C++ library should no be in a .c file. That indicates C language. C++ source modules should generally have .cpp extension.Then, common to both C and C++ language, a header file should have .h extension to denote that it is a header. Header files are used to define the APIs in your library; i.e., the names of functions and their parameters. The actual bodies of the various functions in your library should be in .cpp files. The API defined in the header file should match the function definitions in the .cpp files.

Including a .c file in a c++ program

I am working on a project where half my code is in c and half is in c++.
Now I want to include the cprogram.c in my cppprogram.cpp.
When including a c file inside a c program you can use
#include "cprogram.c"
Anyways to do that in c++ for including a c program.
Besides from that you normally don't include .c files, but .h files instead, and that the following is bad practice, you can include C files into .cpp / .hpp files by wrapping the include into an extern "C":
cppprogram.cpp
extern "C" {
#include "cprogram.c"
}
See here for some examples.
As long as you are using Make or Cmake, you can just add it with #include "cprogram.c". C++ is also compatiable with C so you can just add it.

in a c++ project (files are mostly.cpp) ,if a file contains c code only,should it be named as .c or .cpp?

I have been thinking this problem for a while but still no idea about it, if my project is mainly cpp file, should a c file name as .c, or should be named as .cpp to consistent with other .cpp file?
I just list some advantage and disadvantage (in my current knowledge) of using .c (I don't know if the following idea is correct):
advantage of .c:
fast to know it does not contain c++ content (e.g.:class,std::string)
easy to separate from .cpp file by searching name
disadvantage of .c:
not consistent with other files (because other files mostly .cpp)
may need to rename it as .cpp if I want to change the function as using oop or want to add some oop features into it
some scripts or files may need to add *.c as file input if the original version only handles *.cpp, (e.g.: need to add *.c in Android.mk in android jni)
Also I don't know if compiler handles .c and .cpp in different way,also don't know if it affects other behaviour (e.g.:performance,platform or compiler specific issues...), is anyone have idea about it?
Depends what you mean by "C" code.
Are you going to compile it with a C compiler?
Call it file.c
Or do you just mean "C-like" C++ code? C++ code that, at time of writing, happens to also be valid C?
Call it file.cpp
Rule of thumb - name it according to which compiler you intend to use for it. This keeps your makefiles nice and simple.
So if your "C code" is C++ code that could be compiled as valid C but that's not what you are doing, then name it *.cpp and let your makefile invoke the C++ compiler on it.
If your code is actual C, to be compiled with a C compiler, then name it *.c - and remember the (appropriately-#ifdefed) extern "C" in the header file so that C++ built against it can link successfully.
C++ fully supports c code. So the compiler would be just fine with c code in a .cpp file.
And like Quentin mentioned above. If your c code is never used in a c only project I would leave it in an cpp file.

How to #import C++ Header File in iPhone/iPad Objective C Project

Referred Question: Problem when #import C++ Header File in iPhone/iPad Project
Well, my project is relatively huge and cannot solve that either changing ALL or PART OF my project files .m to .mm or changing the ALL or PART OF file types in File Inspector.
That is because both my colleagues and library authors DOES NOT follow the C++ standard strictly, such as assigning void* to int* and other various violations. Objective C would allow these and just gives warnings.
The C++ header file I want to #import is a library. It uses keyword namespace in its header, while its implementation is in an .a assembly file. That means it is nearly impossible to hack them. Besides that, my project also includes other libraries that are compatible only with Objective C.
Does anyone know how to solve this?
The ways I could imagine is as follows:
Find an alternative for namespace, but still I want to write
codes like QCAR::Renderer in my project.
Tell the compiler to recognized C++ header(Well, that might not be
possible)
EDIT
#ifdef __cplusplus
# include MyCPPHeader.h
#endif
If I use that, would MyCPPHeader.h really get included in an Objective-C environment? I guess not. And that's against the principles of not hacking libraries.
EDIT
Even I changed these .mm files to include that C++ Header, i would get an link error saying Undefined symbols for architecture armv7:. This happens when my .mm files including .h headers in other libraries.
I have solved this a year ago, but for other people who are looking for an answer, here is the solution.
Say the C++ style header is named cpp.h. In my project, I never #import "cpp.h". I wrote a C style header, named c.h, as a wrapper header for cpp.h. Then I wrote a C++ implementation source file, named c.mm to implement the function define in c.h by calling functions in cpp.h. If I want to call functions in cpp.h, I #import c.h and use the wrapper function instead.
Below is the explanation in a possibly clearer way:
cpp.h C++ style header file using the namespace, its implementation is in assembly code so it is hard to change
c.h C style header as the wrapper for cpp.h
c.mm C++ implementation implements functions in c.h by calling functions in cpp.h
#import "c.h" to use the wrapper functions