Merge c and c++ code, questions....

C

camelot

Hello,
I’ have a curiosity. I wrote this two files:

1) testmain.c

#include <stdio.h>
extern void myfunc();
int main(int argc, char* argv[])
{
myfunc();
}

2) myfunc.cpp

#include <string>
extern "C" void myfunc()
{
string a;
}

Then I compile:

g++ -c myfunc.cpp obtaining myfunc.o

and

gcc myfunc.o testmain.c –o testmain

obtaining a lot of Symbol referencing errors!

Question1 – it is possible, through some particular compiling
tecninques, to merge this two sources?
Question2 – when someone says that it is possible to merge c and c++
program, under which conditions this statement it is true?

Thank you,

Regards,

Camelot
 
G

gwowen

gcc myfunc.o testmain.c –o testmain

Use g++ instead of gcc at this stage.
This automatically links the required C++
libraries, and resolves the internal C++
symbols declared in <string>.

Nitpick: use std::string , not just string
to declare a;
 
C

camelot

Use g++ instead of gcc at this stage.
This automatically links the required C++
libraries, and resolves the internal C++
symbols declared in <string>.

Nitpick: use std::string , not just string
to declare a;

yes, but my question was if I could compile final stuff with gcc. Is
it possible?
The original problem born becaouse I'have a huge C framework (hardly
compilable with g++) and I want to add some c++ code...

Camelot
 
A

Alf P. Steinbach

* camelot:
Hello,
I’ have a curiosity. I wrote this two files:

1) testmain.c

#include <stdio.h>
extern void myfunc();
int main(int argc, char* argv[])
{
myfunc();
}

2) myfunc.cpp

#include <string>
extern "C" void myfunc()
{
string a;

This should not compile.

}

Then I compile:

g++ -c myfunc.cpp obtaining myfunc.o

No you do not.

g++ would have reported an error.

So the statement is at best an incorrect impression.

and

gcc myfunc.o testmain.c –o testmain

obtaining a lot of Symbol referencing errors!

Question1 – it is possible, through some particular compiling
tecninques, to merge this two sources?

See the FAQ.

Question2 – when someone says that it is possible to merge c and c++
program, under which conditions this statement it is true?

Generally 'main' must be C++.


Cheers & hth.,

- Alf
 
G

gwowen

yes, but my question was if I could compile final stuff with gcc. Is
it possible?
The original problem born becaouse I'have a huge C framework (hardly
compilable with g++) and I want to add some c++ code...

Compile the C source to object code with gcc
Compile the C++ source to object with g++

Finally, Link the objects with g++.

If you have objects compiled with g++, you need to link them
with g++, but you can freely use g++ to also link them with
compiled-from-C objects.

If the C++ code calls C functions, the C++ compiler will need
extern "C" declarations on the prototypes.
 
B

Bart van Ingen Schenau

camelot said:
yes, but my question was if I could compile final stuff with gcc. Is
it possible?
The original problem born becaouse I'have a huge C framework (hardly
compilable with g++) and I want to add some c++ code...

Camelot

It is possible to combine C and C++ code (compiled with respectively a C
and a C++ compiler) in one executable, under the following conditions:
- The function main() must be written in C++
- The C++ standard library gets linked and its start-up code is used to
invoke main(). This is done automatically when you allow g++ to invoke
the linker.

If your existing C framework already has a main() function, the easiest
way to meet the first condition above is:
- Rename the existing main() to real_main
- Add this C++ file to the project:
// main.cpp
extern "C" int real_main(int, char**);
int main(int argc, char** argv)
{
return real_main(argc, argv);
}
//-- EOF

Bart v Ingen Schenau
 
J

James Kanze

yes, but my question was if I could compile final stuff with
gcc. Is it possible?

If there is any C++ in the application, the final link should be
with g++ (and main should probably be in C++).
The original problem born becaouse I'have a huge C framework
(hardly compilable with g++) and I want to add some c++
code...

If the source filename ends with .c, g++ (like most other C++
compilers) will compile it as C. There's no problem mixing the
two languages, as long as main is written in C++, and g++ is
used to compile the C++ and for the final link. (For the
sources in C, you can use g++ or gcc, indifferently, to compile
them.)

I might add that this is more or less the case with all
compilers I know.
 
R

Rolf Magnus

James said:
If the source filename ends with .c, g++ (like most other C++
compilers) will compile it as C.

That's not true. g++ always compiles the code as C++. But gcc will compile
the code as C++ if it ends in .C, .cpp, .cc or .cxx (possibly also others).
However, that's only true for compiling. For linking, you need to use g++
explicitly (or gcc -x c++).
 
R

Rolf Magnus

James said:
So I see. I'd consider that a serious defect.

Uhm, why? g++ is supposed to be the C++ compiler and therefore always
assumes C++, while gcc is the generic compiler front-end that tries to auto-
guess the language based on the file name.
 
J

James Kanze

Uhm, why? g++ is supposed to be the C++ compiler and therefore
always assumes C++, while gcc is the generic compiler
front-end that tries to auto- guess the language based on the
file name.

G++ isn't a compiler, but a compiler front-end, like gcc. All
it should do is ensure that the extra steps and libraries
necessary for linking C++ are taken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top