C++ compiler using C compiler options

Discussion about CodeLite development process and patches
brian
CodeLite Enthusiast
Posts: 19
Joined: Sat Jun 04, 2011 6:22 pm
Genuine User: Yes
IDE Question: C++
Contact:

C++ compiler using C compiler options

Post by brian »

Perhaps I am doing my setup wrong (being new to CodeLite and Linux) but I need to compile a bunch of C code as C++ to generate a set of static libraries. In the 'settings' option for the project you can configure which compiler you want to use (gcc [C] or g++ [C++] in this case) and then there is a 'compiler' selection where one can set additional command line options. Since this is a static library build (and not an executable) I do not get a list of options to select in these fields (not sure why; I do get them if I am making an executable). So I just type them in their respective boxes.

I needed to add the -fpermissive option to the g++ command line. However, if I added it to the C++ command line option, it was not used. On the other hand, if I added it to the C command line option it was used. Looking at the build log showed that the compiler WAS g++ in both cases and not gcc. So it appears that g++ is being invoked due to the general settings but that the compiler options are nevertheless taken from the C entry and not the C++ entry.

I am not familiar enough with gcc or g++ to know if the resultant command line is correct as there are default options (thank god!) provided by CodeLite.
I am having a terrible time building this C code as C++ (builds fine as C) and was told that the C-code libraries build on Linux as C++. So I am concerned that perhaps the compilation problem might be due to analogous confusions in the settings of the default values; they are being taken from the C-settings and not C++. Has anyone else had issues with confusions between C and C++ build options?
josee
CodeLite Enthusiast
Posts: 37
Joined: Fri Feb 05, 2010 10:57 am
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by josee »

If you want to use your static C library from within another C++ project it is not necessary
to built it (your static lib) with g++. Use gcc and link it to your C++ project.

The only one thing you have to remember is to wrap alle your C header files in a

#ifdef __cplusplus
extern "C" {
#endif

... YOUR C HEADER DECLARATIONS goes here ...

#ifdef __cplusplus
}
#endif

With the gnu compiler instead you can use

#include <sys/cdefs.h>

__BEGIN_DECLS

... YOUR C HEADER DECLARATIONS goes here ...

__END_DECLS
brian
CodeLite Enthusiast
Posts: 19
Joined: Sat Jun 04, 2011 6:22 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by brian »

So in CodeLite I would select the gcc compiler and define the __cplusplus preprocessor variable and then still build in C. (All 200 little files already have the __cplusplus thing in there).

I will try that and see if the projects build and that they successfully link with libs built using C++.
brian
CodeLite Enthusiast
Posts: 19
Joined: Sat Jun 04, 2011 6:22 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by brian »

Interesting: Here is what I tried. In CodeLite I set the General/Compiler setting to 'gnu gcc' (alternative is 'gnu g++)
I set the Compiler/PreProcessor to nothing. The project builds and the build window shows:
gcc -c "/home/brian/oxp_lib/til/src/til.c" -g -o ./Debug/src_til.o -I. -I. -I../include
I take it this option makes a library that is not usable in a C++

I set the General/Compiler setting to 'gnu gcc' and Compiler/PreProcessor '__cplusplus'. Get the following:
gcc -c "/home/brian/oxp_lib/til/src/til.c" -g -D__cplusplus -o ./Debug/src_til.o -I. -I. -I../include
and the build fails all over. The first fail is in <stdio.h> on line containing __BEGIN_DECLS
I take it this option WOULD make a library that is usable in a C++ if it would build?

I set the General/Compiler setting to 'gnu g++' and Compiler/PreProcessor nothing
g++ -c "/home/brian/oxp_lib/til/src/til.c" -g -o ./Debug/src_til.o -I. -I. -I../include
and the build is successful
I take it this option makes a library that is usable in a C++?

Boy am I confused.

I have modified the code to get rid of all void * casts.
josee
CodeLite Enthusiast
Posts: 37
Joined: Fri Feb 05, 2010 10:57 am
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by josee »

Yes, you are confused.

You do not know that __cpluplus is a macro which is defined by a C++ compiler automatically.
A C compiler does not define it. It is used to differentiate in header files between the two types.

Try to contact a C++ beginners forum which can help you better.
brian
CodeLite Enthusiast
Posts: 19
Joined: Sat Jun 04, 2011 6:22 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by brian »

That's what I had assumed;
That means I MUST use the g++ compiler so that the __cplusplus macro is invoked (though CodeLite will let me force it). But this statement
'If you want to use your static C library from within another C++ project it is not necessary
to built it (your static lib) with g++.' threw me for a loop, since building with gcc gave me a library that I could not use with the libraries built in C++. None of the references were recognized.

Whats confusing me is g++ and gcc and how they work. I have been spoiled by Visual Studio where everything is taken care of under the hood.

All my C project libraries already have the
#ifdef __cplusplus
extern "C" {
#endif

... YOUR C HEADER DECLARATIONS goes here ...

#ifdef __cplusplus
}
#endif

in then and my C++ libraries; well, they don't need it. Then in VS I simply request that the C projects are built using C++ option. Presto, it works and then I can use all the libraries in my C++ projects. (I have no idea what the underlying command line looks like but I have seen a quick glimpse and it is ugly!)

The only reason I even have mixed code is that there are a massive number of ASN1 structures in the project and those structures are built using an ASN1 compiler that creates C code.

In the end I was hoping that CodeLite would 'protect' me from the nitty-gritty of the command line details and syntax and makefile complexities in the same manner as Visual Studio, so that all I have to do is think 'here are my project files, here is what I need to do' and click the right selections in the options and CodeLite would create the correct command line syntax from the options.

So I think I was on the right track to start with. Have CodeLite build the C projects using g++ and go from there...I just have to get the C code to build with g++ and that is not easy.
brian
CodeLite Enthusiast
Posts: 19
Joined: Sat Jun 04, 2011 6:22 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: C++ compiler using C compiler options

Post by brian »

Moat of my issues were solved by fixing the settings. The g++ setting had the g++ used for C compilations instead gcc.
With the setting correct almost all of the issues and confusion raised vanished. It was nice. Things happened as
expected.

See topic "Bug Settings".
Post Reply