How to link obj's from c-source with obj's from c++-source

Discussion in 'C++' started by Guido Belligoi, Oct 19, 2006.

  1. Hi,

    I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c.
    test.c contains just a simple function, which is declared in test.h.
    test.h is included in test.c and in main.cpp.
    When I try to link the objects with link main.cpp test.c -out:prog.exe
    I get the error:
    "main.obj : error LNK2019: unresolved external Symbol "int __cdecl
    greater(int,int)" (?greater@@YAHHH@Z), called in function _main
    prog.exe : fatal error LNK1120: 1 unresolved external Symbol"

    What have I to do, to be able to link those objects together?

    Guido Belligoi
     
    Guido Belligoi, Oct 19, 2006
    #1
    1. Advertising

  2. "Guido Belligoi" <> wrote in message
    news:...
    : Hi,
    :
    : I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c.
    : test.c contains just a simple function, which is declared in test.h.
    : test.h is included in test.c and in main.cpp.
    : When I try to link the objects with link main.cpp test.c -out:prog.exe
    : I get the error:
    : "main.obj : error LNK2019: unresolved external Symbol "int __cdecl
    : greater(int,int)" (?greater@@YAHHH@Z), called in function _main
    : prog.exe : fatal error LNK1120: 1 unresolved external Symbol"
    :
    : What have I to do, to be able to link those objects together?

    A: extern "C"


    For headers that are to be included both from C and C++ sources,
    it is common to surround all declarations with:

    #ifdef __cplusplus
    extern "C" {
    #endif

    int greater(int a, int b);

    #ifdef __cplusplus
    }
    #endif


    You could also include an originally C-only header with:
    extern "C" {
    #include "test.h"
    }


    IIRC it is also possible to declare an individual function
    as extern C:

    extern "C" int greater(int a, int b);



    hth-Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Oct 19, 2006
    #2
    1. Advertising

  3. Ivan Vecerina schrieb:


    a very helpfully answer!

    Thank you for the fast reply - it works fine now!!!

    Guido Belligoi
     
    Guido Belligoi, Oct 19, 2006
    #3
    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. Mark Kamoski
    Replies:
    3
    Views:
    15,754
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    476
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    447
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    512
    Peter Otten
    May 4, 2005
  5. alf
    Replies:
    9
    Views:
    414
Loading...

Share This Page