collect2: ld returned 1 exit status

B

bobrics

Hi,

I am trying to link use a library created by me, and getting the
following error during linking:

$: sudo gcc -c mycode.c
$: sudo ar rc libmylib.a myfuncs.o
$: sudo runlib libmylib.a
$: gcc myfuncs.o -o mycode -L. -lmylib -lm
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/crt1.o: In function
`_start':
.../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

What is the problem?

Thank you in advance
SB
 
B

Ben Pfaff

bobrics said:
/usr/lib/gcc/i486-linux-gnu/4.0.2/../../../../lib/crt1.o: In function
`_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'

Does your program have a main() function?
 
B

bobrics

I think I found my mistake. I am missing the mycode.c source filename
that contains main function.

$: gcc -o mycode -L. -lmylib myfuncs.o -lm
Should be:
$: gcc mycode.c -o mycode -L. -lmylib myfuncs.o -lm

I have another error, but that's due to global variable definition.
One of the functions that I have added into a library, modifies a
global variable that is defined in the main code (mycode.c). So, the
compiler complains that it's not defined inside mycuncs.c file. If I
will define it there, there'll be a double definition.
Is it still possible to do that (use globals and store functions that
modify these globals in a library) and how? I know it's a bad habit,
but there's some code that I need to adapt, but do not want to change
it.

Thank you
 
B

bobrics

So, I have modified the gcc line to:
$: sudo gcc mycode.c -o mycode -L. -lmylib myfuns.o
.... to specify mycode.c file. Below are the mycode.c and myfuncs.c
files. Here is the new error that I am getting:

$ sudo gcc mycode.c -o mycode -L. -lmylib myfuns.o
mycode.c: In function 'main':
mycode.c:7: warning: return type of 'main' is not 'int'
/tmp/ccgzNw0T.o: In function `main':
mycode.c:(.text+0x1d): undefined reference to `myfun1'
mycode.c:(.text+0x22): undefined reference to `myfun2'
collect2: ld returned 1 exit status

So, even though I am declaring the prototypes in mycode.c file as
extern functions, the compiler still complains.


AND here are my files.
---> mycode.c
#include <stdio.h>

extern void myfun1();
extern void myfun2();


void main() {
myfun1();
myfun2();
}


---> myfuncs.c
#include <stdio.h>

void fun1() {
printf("This is fun1()\n");
}

void fun2() {
printf("This is fun2()\n");
}
 
A

Andrew Poelstra

bobrics said:
I think I found my mistake. I am missing the mycode.c source filename
that contains main function.

$: gcc -o mycode -L. -lmylib myfuncs.o -lm
Should be:
$: gcc mycode.c -o mycode -L. -lmylib myfuncs.o -lm

I have another error, but that's due to global variable definition.
One of the functions that I have added into a library, modifies a
global variable that is defined in the main code (mycode.c). So, the
compiler complains that it's not defined inside mycuncs.c file. If I
will define it there, there'll be a double definition.
Is it still possible to do that (use globals and store functions that
modify these globals in a library) and how? I know it's a bad habit,
but there's some code that I need to adapt, but do not want to change
it.

Thank you


Ben said:
Does your program have a main() function?

Don't top post, and you can use the "extern" keyword before all other
definitions other than the first one, I think. It's been a long time
since I used multi-file variables, or even global-scope variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top