delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/16/23:29:26

From: jlrubin AT bway DOT net (Josh Rubin)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! I cant link c-source with c++-source
Date: Tue, 16 Jun 1998 14:52:25 GMT
Organization: ISPNews http://ispnews.com
Lines: 37
Message-ID: <35868313.1231088@news1.bway.net>
References: <35864D37 DOT 69F2 AT efd DOT lth DOT se>
NNTP-Posting-Host: dial-iris-34.bway.net
Mime-Version: 1.0
NNTP-Posting-Date: 16 Jun 1998 14:52:29 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Tue, 16 Jun 1998 12:47:20 +0200, Peter Danielsson
<e96pd AT efd DOT lth DOT se> wrote:

>I'm using allegro and it is c. I'm more used to c++, so i tried to have
>the mainprogram in one file with the extension .c and wanted to call my
>functions in a .cpp-file. The gcc-compilation works good, but when
>linking it cant find my functions in the .cpp-file. What must I do to
>link two different sources?

You must tell the C++ compiler which functions were
compiled as C. Otherwise it does name mangling, and the
linker will fail to find the C definition.

This is standard C++, and should be described in a good book.

In file main.cc:

extern "C"  { 
  struct mystruct { ... };      // declare types used to communicate
                                        // between C and C++.   
  int func(int x);                 // declare functions compiled as C
}

in file.c :

int func(int x)         // no special declarations
{
....
}

You can get fancy and write a header file that
puts in the  extern "C" when compiled as C++
and omits it when compiled as C.


Josh Rubin
jlrubin AT bway DOT net

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019