By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

How to compile using library

P: n/a
Hello

I'm new in C, And I want to compile program which use extern function.
How to do it because I always got error.
Undefined first referenced
symbol in file
greet /tmp/cchAXHfp.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

first I did "gcc -G -o libgreet.so greet.c" and it is ok.
Later I tried:
gcc -G -lgreet mix.c
gcc -L/local/data02/app/repapp/test/gp
-I/local/data02/app/repapp/test/gp mix.c
gcc -L/local/data02/app/repapp/test/gp mix.c

And always I got error.

Thx
Robert

bash-2.05$ cat greet.h
char *greet();

bash-2.05$ cat greet.c
#include "greet.h"

char *greet() {
return ((char *) "Hello!");
}
bash-2.05$ cat mix.c
#include <stdio.h>
extern char *greet();
int main() {
char *greeting = greet();
printf ("aa\n");
return (0);
}

Dec 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> I'm new in C, And I want to compile program which use extern function.
How to do it because I always got error.
Undefined first referenced
symbol in file
greet /tmp/cchAXHfp.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

first I did "gcc -G -o libgreet.so greet.c" and it is ok.
Later I tried:
gcc -G -lgreet mix.c
gcc -L/local/data02/app/repapp/test/gp
-I/local/data02/app/repapp/test/gp mix.c
gcc -L/local/data02/app/repapp/test/gp mix.c


<-- snip code -->

Simply linking both files to one executable :

$ gcc -c greet.c -o greet.o # compile file 1
$ gcc -c mix.c -o mix.o # compile file 2
$ gcc mix.o greet.o -o greet # link both object files into binary
$ ./greet # run binary

--
:wq
^X^Cy^K^X^C^C^C^C
Dec 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.