Insert C code chunk in a big C++ program

H

horacius.rex

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

On machine Y I can do:

1- compilation of Main.cpp to object file Main.o using compiler Y++
2- compilation of function.c to objetc file function.o using compiler
Ycc

Compilers Y++ and Ycc come from the same company. "Y" could be for
instance the GNU compiler so we would have g++ and gcc.

3- Main.o sends input data to function.o and function.o returns
calculated data to Main.o. Now I link in some way Main.o with
function.o and create Program_in_Y.exe.

My general question is if I can do the same procedure in machine X.
Perhaps the same question is if objects created with different
compilers on the same machine can link in some way and create an
executable or binary file.

Thanks.
 
P

popyart

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

On machine Y I can do:

1- compilation of Main.cpp to object file Main.o using compiler Y++
2- compilation of function.c to objetc file function.o using compiler
Ycc

Compilers Y++ and Ycc come from the same company. "Y" could be for
instance the GNU compiler so we would have g++ and gcc.

3- Main.o sends input data to function.o and function.o returns
calculated data to Main.o. Now I link in some way Main.o with
function.o and create Program_in_Y.exe.

My general question is if I can do the same procedure in machine X.
Perhaps the same question is if objects created with different
compilers on the same machine can link in some way and create an
executable or binary file.

Thanks.
 
B

Branimir Maksimovic

Technically if both compilers produce same object format that
can link, only problem that remains is calling convention.
You can see if you can specify calling convention of interfacing
functions explicitelly. Another problem is, as if compilers don;t
have compatible calling conventions at all.
Then you are out of luck, as then you have to write thunks in
assembler that will forward parameters
and result between functions.
And of course you have to make sure that layout for parameters
and results match. eg long double for one compiler can be
64 bit and for other 80 bit, not to mention structs.
All in all tough luck. If you are not experienced in assembler
programming then it is easier to just compile both Main.cpp and
function.c (with possible corrections) with XA++.

Greetings, Branimir.
 
L

llothar

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

On machine Y I can do:

1- compilation of Main.cpp to object file Main.o using compiler Y++
2- compilation of function.c to objetc file function.o using compiler
Ycc

Compilers Y++ and Ycc come from the same company. "Y" could be for
instance the GNU compiler so we would have g++ and gcc.

3- Main.o sends input data to function.o and function.o returns
calculated data to Main.o. Now I link in some way Main.o with
function.o and create Program_in_Y.exe.

My general question is if I can do the same procedure in machine X.
Perhaps the same question is if objects created with different
compilers on the same machine can link in some way and create an
executable or binary file.

Thanks.

Ah, a good homework question!

Normally every platform has a ABI which tells how to pass parameters
to a function etc.
If the compiler A obeys to this ABI and compiler 'B' does not you have
a problem otherwise
it might work (if the runtime functions are also compatible). You
might also have different
identifier names like preceding underscores.

For more information please use the references in the books list your
teacher gave you.
 
B

Barry

Branimir Maksimovic said:
Technically if both compilers produce same object format that
can link, only problem that remains is calling convention.
You can see if you can specify calling convention of interfacing
functions explicitelly. Another problem is, as if compilers don;t
have compatible calling conventions at all.
Then you are out of luck, as then you have to write thunks in
assembler that will forward parameters
and result between functions.
And of course you have to make sure that layout for parameters
and results match. eg long double for one compiler can be
64 bit and for other 80 bit, not to mention structs.
All in all tough luck. If you are not experienced in assembler
programming then it is easier to just compile both Main.cpp and
function.c (with possible corrections) with XA++.

Greetings, Branimir.
<OT>
"just compile both Main.cpp and
function.c (with possible corrections) with XA++"

This is probably the best guess solution. But you
can omit the corrections part if the compiler is also
a C compiler and the code is actually C.

This discussion is completely off topic for clc. So if you
really want an answer you should look at the documentation
for the compilers you plan to use.
</OT>
 
B

Branimir Maksimovic

<OT>
"just compile both Main.cpp and
function.c (with possible corrections) with XA++"

This is probably the best guess solution. But you
can omit the corrections part if the compiler is also
a C compiler and the code is actually C.

This reminds why is bad to answer messages that are
crossposted ;)
This discussion is completely off topic for clc. So if you
really want an answer you should look at the documentation
for the compilers you plan to use.

I guess OP already did that, but yet he/she don;t knows the answer.
OTOH this naive question, speaks that either OP is totally
clueless or is perfect troll ;)

Greetings, Branimir.
 
D

David Wade

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

On machine Y I can do:

1- compilation of Main.cpp to object file Main.o using compiler Y++
2- compilation of function.c to objetc file function.o using compiler
Ycc

Compilers Y++ and Ycc come from the same company. "Y" could be for
instance the GNU compiler so we would have g++ and gcc.

3- Main.o sends input data to function.o and function.o returns
calculated data to Main.o. Now I link in some way Main.o with
function.o and create Program_in_Y.exe.

My general question is if I can do the same procedure in machine X.
Perhaps the same question is if objects created with different
compilers on the same machine can link in some way and create an
executable or binary file.

There is no gaurentee you can do this with arbitary comilers, though in
practice its often possible. Consider the case for machines where there is
no natural stack, e.g. IBM370. Even of both implementations use the same
register as the stack pointer, one could build the stack from low memory up,
another from high memory down....
 
T

Thomas J. Gritzan

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

To a platform specific or compiler specific newsgroup.
My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

In general, object files from two different compilers from two different
companies don't link together.

Why didn't you try if compiler Y can compile for your platform X also? If
it is GNU g++/gcc, if can compile for many platforms.
 
I

Ian Collins

Hi,

I don't know if it is better to post this question to a C or C++
group, so my apologies.

My general question:

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

On machine Y I can do:

1- compilation of Main.cpp to object file Main.o using compiler Y++
2- compilation of function.c to objetc file function.o using compiler
Ycc

Compilers Y++ and Ycc come from the same company. "Y" could be for
instance the GNU compiler so we would have g++ and gcc.

3- Main.o sends input data to function.o and function.o returns
calculated data to Main.o. Now I link in some way Main.o with
function.o and create Program_in_Y.exe.

My general question is if I can do the same procedure in machine X.
Perhaps the same question is if objects created with different
compilers on the same machine can link in some way and create an
executable or binary file.
Provided the C functions are declared as extern "C" to the C++ code, you
shouldn't have a problem. It is standard practice for all native C
compilers to use the came calling conventions on a particular machine.
A C compiler that didn't would be next to useless as it wouldn't be able
to generate code that used the native libraries.
 
E

Ernie Wright

Ian said:
It is standard practice for all native C compilers to use the came
calling conventions on a particular machine. A C compiler that didn't
would be next to useless as it wouldn't be able to generate code that
used the native libraries.

One might think so, but I know of at least one counterexample, and it
certainly wouldn't surprise me if there were others.

The Win32 API contains no function that returns a floating-point value,
so no convention arose about how to do that. Given the following,

double foo( void );
double result;
result = foo();

the returned value is handled by Microsoft Visual C++ and Watcom 10.0
in different ways.

MSVC: call foo
fstp result ; pop ST(0) into result

Watcom: call foo
mov result, eax ; move edx:eax into result
mov result+4, edx

- Ernie http://home.comcast.net/~erniew
 
I

Ian Collins

Ernie said:
One might think so, but I know of at least one counterexample, and it
certainly wouldn't surprise me if there were others.

The Win32 API contains no function that returns a floating-point value,
so no convention arose about how to do that. Given the following,
So what about strtod() and friends?
 
K

Keith Thompson

CBFalconer said:
This is really a C++ question, and should be answered on
comp.lang.c++. However the secret is in the use of __cplusplus__
in the header file for function.c. However you also need to check
the documentation of your compilers.

It's __cplusplus, not __cplusplus__.
 
E

Ernie Wright

Ian said:
So what about strtod() and friends?

Not sure what you're asking. They aren't part of the Win32 API.

In general, compilers for Windows provide their own C runtime libraries.
It'd be pretty unusual for code compiled by X to be linked to Y's
runtime, and I wouldn't automatically assume that it could be done.

- Ernie http://home.comcast.net/~erniew
 
I

Ian Collins

Ernie said:
Not sure what you're asking. They aren't part of the Win32 API.

In general, compilers for Windows provide their own C runtime libraries.
It'd be pretty unusual for code compiled by X to be linked to Y's
runtime, and I wouldn't automatically assume that it could be done.
I see. I didn't realise that, I'm used to operating systems which have
their own standard libraries.
 
T

Thad Smith

I have a very big C++ program (Main.cpp) which compiles on machine X
with compiler XA++.

I also have a function whose source code (function.c) in C (not C++)
compiles on machine X with compiler XBcc.

XA++ and XBcc are compilers from different companies.

Does XA++ have a C mode for compiling?
 
H

horacius.rex

Doing some investigation I checked that using GNU compiler for C++ and
C with function wrappers, this is feasible.

Thanks.
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top