Thread: combining c with c++ code

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    combining c with c++ code

    Hello, I downloaded code written in c which consists of many individual files and routines. I would like to use his code to create a plugin for a 3d animation program.

    I program graphics using in C++ and only program for my software's API. To be honest with you, I'm not really sure how to go about it. Would it be possible to create a project as I normally do and import all the c and header files into a c++ project? Or do I have to compile his code? And if I do, what do I compile to? a dll or exe or something else? If I do, how do I access those routines?

    As you can probably tell, I am very new at this. Any suggestions will be appreciated.

    verb

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To access a function written in C from C++, you just have to put extern "C" around the function. That's it basically.
    Code:
    extern "C" void function() {}
    You can use extern "C" with curly braces to enclose a number of functions.

    If the code was written to support C++, it might have something like this in the header file(s):
    Code:
    /* at the beginning of the file */
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* ... the function declarations ... */
    
    #ifdef __cplusplus
    }
    #endif
    If not, you can add that yourself, or do it in your C++ code where you include the header file.
    Code:
    extern "C" {
    #include "header.h"
    }
    You should then be able to include the .c files in your project and compile them normally. (A C++ compiler will likely be able to compile the C code.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Hi I downloaded c code that I am now using in my c++ plugin code.
    I managed to tie both c and c++ code using the extern "C"{} construct.

    I am getting these error messages. There is a main proc in every c file and I think that is confusing my compiler. Does anyone know how to resolve this error message?

    ------ Build started: Project: compCal, Configuration: Debug Win32 ------
    Linking...
    cal_fo.obj : error LNK2005: _main already defined in cal.obj
    csyn.obj : error LNK2005: _main already defined in cal.obj
    ecal.obj : error LNK2005: _main already defined in cal.obj
    ic2wc.obj : error LNK2005: _main already defined in cal.obj
    ncal.obj : error LNK2005: _main already defined in cal.obj
    ncal_fo.obj : error LNK2005: _main already defined in cal.obj
    ncsyn.obj : error LNK2005: _cc_generate_calibration_constants already defined in csyn.obj
    ncsyn.obj : error LNK2005: _nc_generate_calibration_constants already defined in csyn.obj
    ncsyn.obj : error LNK2005: _generate_calibration_data already defined in csyn.obj
    ncsyn.obj : error LNK2005: _main already defined in cal.obj
    ncsyn.obj : error LNK2005: _idum already defined in csyn.obj
    wc2ic.obj : error LNK2005: _main already defined in cal.obj
    xfd2xfu.obj : error LNK2005: _main already defined in cal.obj
    Creating library Debug/compCal.lib and object Debug/compCal.exp
    cal_main.obj : error LNK2019: unresolved external symbol _lmdif_ referenced in function _cc_compute_exact_f_and_Tz
    ecalmain.obj : error LNK2001: unresolved external symbol _lmdif_
    Debug\compCal.dll : fatal error LNK1120: 1 unresolved externals

    tim
    Last edited by verbatimline; 07-17-2008 at 03:39 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm . . . you can't compile a bunch of files each with a main() and expect it to work, as you have seen.

    If you're sure that the code will actually work together -- which I highly doubt -- you could try remaining the main() functions in each of the files and somehow creating a main() of your own which calls all of those functions. But that would execute each one sequentially, which may not be what you want.

    I guess the short answer is, rename the main() functions to something else. Whether you'll actually be able to use the resulting code is another matter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. Combining C && C++ Code
    By drtriedl in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 01:48 PM