g++ linking to gcc compiled libraries

Frederich, Eric P21322 eric.frederich@siemens.com
Wed Oct 3 17:47:00 GMT 2007


Hello guys.
I'm hoping you can tell me what I'm doing wrong here.
I have a library written in C that I'd like to link to from both c
programs and c++ programs.
I'm guessing that this is possible.

If I compile both the library and program using g++ it works fine, but
then I can't link to the c library from c programs.

Here is the output of me trying to use g++ to link to a c library
compiled with gcc.
What am I doing wrong?  Or is it even possible to do?

$ cat clibrary.h
int cfun(int d);
$ cat clibrary.c
int cfun(int d){
    return d*100 + 7;
}
$ cat cppsource.cc
#include <iostream>
using namespace std;

#include "clibrary.h"

int main(int argc, char* argv[]){
    int myint = cfun(3);
    cout << myint << endl;
}
$ gcc -c clibrary.c
$ g++ -c cppsource.cc
$ g++ -o cppsource.exe cppsource.o clibrary.o
cppsource.o(.text+0x83): In function `main':
: undefined reference to `cfun(int)'
collect2: ld returned 1 exit status
$

Thanks,
~Eric



More information about the Gcc-help mailing list