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.
Related
I have a library written by a not very meticulous coder, which includes the following code in a C header file:
/* SomeCHeaderFile.h */
...
#define local static
#define package
#define global
When this file is included in a C++ header, for example:
EDIT: I forgot to mention that I include the header like so:
// SomeCPlusplusSourceFile.cpp
extern "C" {
#include "SomeCHeaderFile.h"
}
...
the compiler gives the following error:
error: constructor cannot be static member function
error: 'std::locale::locale(const std::locale&)' cannot be overloaded
error: with 'std::locale::locale(const std::locale&)'
I only say the coder was not meticulous because she/he never tested it in a C++ code. But I don't know the reasoning behind this to why is this causing the build to fail?
gcc version 4.4.3 | ubuntu linux
It seems the troublesome C header file is redefining tokens in use by the standard C++ header files. If you want to use the C header file in your C++ code, you may be required to include it after the standard header files to prevent this kind of problem.
This may not sufficiently guard you from problems if the C++ code defines its own macros that also redefine the same tokens. If that happens, you will have to segregate your C++ code with files dedicated to C++ that uses the troublesome C header file and C++ code that does not. The C++ code that does use the troublesome C header file makes sure to not use any C++ header file that would cause problems.
You can use C header files in CPP files as follows:
extern "C" {
//Headers go here
}
More details here:
In C++ source, what is the effect of extern "C"?
What I want to do:
I have autogenerated C Code generated with Matlab Simulink and want to enhance it with some more functionality written in C++. To be exact, the C code calls a C-style API that internally uses C++. The whole thing is in a VS 2008 C++ project.
The problem:
It compiles, as long as I tell VS to compile it as C and leave out my C++ code. As soon as I compile it as C++ problems arise.
First of all, I can't compile it as C++ because math.h produces an error C2668 due to an ambiguous call to an overloaded function (fabs()).
If I now additionally add some C++, e.g. include iostream, I get hundreds of compiler errors complaining about missing curly braces and misplaced colons somewhere in cstdlib.
My question:
How can I mix the two languages in a way that works? I read about preprocessor defines (http://www.parashift.com/c++-faq-lite/overview-mixing-langs.html) but I don't know how to apply them correctly to solve my problem.
Any help is greatly appreciated!
It seems you are including C++ headers in your C source code. Probably indirectly by including it in other header files (i.e. the C source include your C++ header, and the C++ header includes other C++ header files).
There are two ways of solving this:
Use the preprocessor to conditionally include the C++ headers only when compiled in C++. This can be done like
#ifdef __cplusplus
# include some_cpp_header
#endif
Don't include C++ headers (directly or indirectly) in your header files. Or better, make a separate header file whose only purpose is to be included in the C source, and which only contains the function prototypes (with extern "C" when compiled as C++) of the API. The body of the header file could look like this
#ifdef __cplusplus
extern "C" {
#endif
void function1(int);
int function2(const char*);
/* More function prototypes */
#ifdef __cplusplus
}
#endif
I recommend the second method.
Include the <iostream> library and then you'll have to compile your code with a c++-compiler.
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
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.
I have a C++ program (.cpp) inside which I wish to use some of the functions which are present inside the C header files such as stdio.h, conio.h, stdlib.h, graphics.h, devices.h etc.
I could include the stdio.h library inside my cpp file as : #include <cstdio>.
How do I include the other library files?
How do I add the graphics.h library?
I'm using Microsoft Visual Studio 6.0 Enterprise Edition and also Turbo C++ 3.0.
For a list of C standard C headers (stdio, stdlib, assert, ...), prepend a c and remove the .h.
For example stdio.h becomes cstdio.
For other headers, use
extern "C"
{
#include "other_header.h"
}
If you put this inside your headers:
#ifdef __cplusplus
extern "C"
{
#endif
// your normal definitions here
#ifdef __cplusplus
}
#endif
Then it will work for both C and C++ without any problem ...
Hope this helps...:)
I'm not sure what you need exactly, but if you want to use old fashioned C functions inside you C++ program, you can easy include them by removing the .h and add a "c" prefix.
for example if you want to include math.h use
#include <cmath>
Just include them inside a extern "C" block an they should work like expected.
You can #include them using their original names. #include <stdio.h> works just fine in C++.