How to compile C and C++ files together in eclipse? - c++

I have a source code containing .c files which is built using ndk build in eclipse Ide. I want to add a .cpp file , in which i need to import my .c files. I get compile time issues when I do the same. Most of the issues are due to type casting to user defined data types. The compilation runs fine, and ndk -build is successful with only .c files
jni/folder1\folder2\folder3\folder4\src\abc.c:963:29: error: invalid conversion from 'int' to 'MY_STATUS_CODE' [-fpermissive]
How can I solve this?!

You can't include C code in a C++ source file unless the C code is valid C++. The compilation error shows that your C code is not valid C++.
You could try to work around the problem by changing your C code to be also valid C++ but that's a waste of time in my opinion. The proper way to solve the error is to not include the C source file but compile it separately. Including a source file in another is bad design anyway so you'll get rid of that smell for free.

Related

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.

Cmake CHECK_INCLUDE_FILES not finding header file

I'm trying to have Cmake check if the file cxxabi.h is available. This file is from the c++ standard library, at least with g++. My current cmake commands look like this:
include(CheckIncludeFiles)
...
check_include_files(cxxabi.h HAVE_CXXABI)
if(HAVE_CXXABI)
...
else(HAVE_CXXABI)
...
endif(HAVE_CXXABI)
When this is executed, I get:
-- Looking for include files HAVE_CXXABI
-- Looking for include files HAVE_CXXABI - not found.
Although the file is available in /usr/include/c++/4.6.4/ and can properly be found by g++ when I compile a c++ code.
I suspect the macro check_include_files uses the C compiler instead of the C++ one to compile a small program that includes the required file, which of course fails since cxxabi.h is a C++ file.
Any idea how to solve that? (i.e. making the macro use the C++ compiler instead of the C one)
As edited in my original question:
Problem solved. There is a different macro for C++ headers, check_include_file_cxx, located in CheckIncludeFileCXX.
There exists another problem with CHECK_INCLUDE_FILES that I recently discovered with MinGW. The file tested was "ddk/ntapi.h". In the CMakeErr.log for this header I got a multiply messages like "DWORD - does not name a type" and so on for all MS types used in this header. Because of this reason the compilation fails and a requested header appears as "not found", whereas it is not true.
This happens because CheckIncludeFile.cxx contains only the requested header, and some headers in MinGW (and probably in the other APIs) does not include in its body all the list of required headers to be compiled in a standalone program that CMake creates.
The solution for this problem is to add absent basic includes into the CMAKE_REQURED_FLAGS, or as a third variable of CHECK_INCLUDE_FILE_CXX:
CHECK_INCLUDE_FILE_CXX("ddk/ntapi.h" VAR "-include windows.h")

cpp compilation issues in iOS project

I have added some objective C files and c++ files to an existing iOS project. When I import a .h file of a .mm file into a .h file with a corresponding .m file I get all kinds of errors.
When I change the .m file of the .h file importing the new code to .mm I then get the same previous errors but with new errors regarding the new .mm file.
Im not really sure how to fix this.
I am using ARC if this has any effect.
It complains about namespace (unknown type), also Unknown name issues with an imported .mm file, expected expression error on a statement containing :: in a .h file of a .mm file. What I dont get is that these files work correctly in the project I copied them over from. I only took the source from the project. And have added header search paths to the directory. I also selected create references to the added directories option.
For some reason the files arent in the compile source section, and when I try to add them, the file selector tree doesnt contain these source files I added. The added directory is also blue instead of yellow, but empty.
As you know, C++ code can only be used from .mm files. When the compiler complains about namespace being an unknown type, it typically means that the Objective C compiler is being used when the Objective C++ compiler should be used. More simply: you have a .m file that needs to be a .mm.
Speaking generally, there are two options you can pursue. Every time an Objective C file complains, change it to a .mm. This is a slippery slope, and generally results in your entire project cascading into a giant ball of Objective C++. The other option is to quarantine the C++, so that only a bare minimum of your Objective C files need the .mm extension.
Obviously, the latter is the better choice. The likely cause of the confusion is #importing a header file that contains C++ code from elsewhere in your app that doesn't actually require C++. If the C++ is an implementation detail of an Objective C class, you can try to keep the C++ out of the class' header file by leveraging Class Extensions. Otherwise, you can create an Objective C class whose only purpose is to wrap the C++ in its own implementation, while exposing a clean header file for other classes to consume.
Basically, If you can keep your headers clean, then you won't have to worry about accidentally indirectly #importing some C++ into an unsuspecting compilation unit.

C++ - error with include <string>

I am very new to c++ and am doing a tutorial. I have copied the tutorial exactly but on compiling get this error:
'String file not found'
for the line #include <string>;
Could someone tell me how to amend this?
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone.
You seem to have found a solution, I'm adding this to clarify why this is happening. Some compilers integrated with IDEs treat .c files as C source code an .cpp (or .cc, .c++, etc.) as C++ code. As you compile a .c file, C++ support isn't included and a C compiler is used instead of a C++ one. And C doesn't have <string> (although it does have <string.h>, but that contains entirely different stuff).
It looks like your compiler isn't correctly or fully installed. The compiler should be able to find its own headers without further effort on your part.
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone. However, I now get 3 Apple Mach-o Linker (Id) errors (?)
As this is different to the original questions I will close this and open a new one
Thanks for all the help!
check the location c:/...../include
If exist string file should reinstall compiler

Objective C syntax inside .cpp file?

I'm trying to copy over some example code into my own project. The example project is iPhoneExtAudioFileConvertTest from the sdk. The example project contains a file called ExtAudioFileConvert.cpp. This file contains what looks like Objective-C code:
assert([NSThread isMainThread]);
The example project runs fine, but the compiler complains about the code above when I build my own project: error: expected primary-expression before '[' token
What's going on here? Obviously there's some way to use objective c bracket syntax in a .cpp file. What do I need to do to make it work?
Change the file extension to .mm for Objective-C++ instead of just .cpp for C++.
The default build settings of iPhoneExtAudioFileConvertTest is Objective-C++.
If you change the settings to According to File Type, you will get the same error message.
So, change the file extension to .mm or change the build settings of your project.