Insert C code chunk in a big C++ program

Discussion in 'C Programming' started by horacius.rex@gmail.com, May 19, 2007.

  1. Guest

    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.
     
    , May 19, 2007
    #1
    1. Advertising

  2. Guest

    On May 19, 4:02 am, wrote:
    > 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.
     
    , May 19, 2007
    #2
    1. Advertising

  3. On May 19, 1:40 pm, wrote:
    > On May 19, 4:02 am, wrote:
    >
    > > 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.

    ........
    > > 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.


    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.
     
    Branimir Maksimovic, May 19, 2007
    #3
  4. llothar Guest

    On 19 Mai, 18:02, wrote:
    > 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.
     
    llothar, May 19, 2007
    #4
  5. Barry Guest

    <OT>: Insert C code chunk in a big C++ program

    "Branimir Maksimovic" <> wrote in message
    news:...
    > On May 19, 1:40 pm, wrote:
    >> On May 19, 4:02 am, wrote:
    >>
    >> > 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.

    > .......
    >> > 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.

    >
    > 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>
     
    Barry, May 19, 2007
    #5
  6. Re: <OT>: Insert C code chunk in a big C++ program

    On May 19, 3:10 pm, "Barry" <> wrote:
    > "Branimir Maksimovic" <> wrote in message
    >
    > news:...
    >
    > > On May 19, 1:40 pm, wrote:
    > >> On May 19, 4:02 am, wrote:

    >
    > >> > 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.

    > > .......
    > >> > 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.

    >
    > > 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 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.
     
    Branimir Maksimovic, May 19, 2007
    #6
  7. David Wade Guest

    <> wrote in message
    news:...
    > 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....

    > Thanks.
    >
     
    David Wade, May 19, 2007
    #7
  8. Ian Collins Guest

    wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, May 19, 2007
    #8
  9. Ernie Wright Guest

    Ian Collins wrote:

    > 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
     
    Ernie Wright, May 19, 2007
    #9
  10. Ian Collins Guest

    Ernie Wright wrote:
    > Ian Collins wrote:
    >
    >> 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,
    >

    So what about strtod() and friends?

    --
    Ian Collins.
     
    Ian Collins, May 19, 2007
    #10
  11. CBFalconer <> writes:
    [...]
    > 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__.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 20, 2007
    #11
  12. Ernie Wright Guest

    Ian Collins wrote:

    > Ernie Wright wrote:
    >>
    >> The Win32 API contains no function that returns a floating-point
    >> value, so no convention arose about how to do that.

    >
    > 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
     
    Ernie Wright, May 20, 2007
    #12
  13. Ian Collins Guest

    Ernie Wright wrote:
    > Ian Collins wrote:
    >
    >> Ernie Wright wrote:
    >>>
    >>> The Win32 API contains no function that returns a floating-point
    >>> value, so no convention arose about how to do that.

    >>
    >> 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.
    >

    I see. I didn't realise that, I'm used to operating systems which have
    their own standard libraries.

    --
    Ian Collins.
     
    Ian Collins, May 20, 2007
    #13
  14. Thad Smith Guest

    wrote:

    > 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?

    --
    Thad
     
    Thad Smith, May 20, 2007
    #14
  15. Guest

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

    Thanks.




    On 19 mayo, 13:02, wrote:
    > 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.
     
    , May 21, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    680
  2. Shaguf
    Replies:
    0
    Views:
    362
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    458
    Shaguf
    Dec 26, 2008
  4. Sanjeeb
    Replies:
    3
    Views:
    423
    Ryan Kelly
    Aug 3, 2010
  5. Shaguf
    Replies:
    0
    Views:
    244
    Shaguf
    Dec 26, 2008
Loading...

Share This Page