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

collect2: ld returned 1 exit status

P: n/a
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

Apr 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"bobrics" <bo*****@gmail.com> writes:
/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?
--
"Am I missing something?"
--Dan Pop
Apr 22 '06 #2

P: n/a
Yes, I do have a main function in mycode.c file
Ben Pfaff wrote:
"bobrics" <bo*****@gmail.com> writes:
/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?
--
"Am I missing something?"
--Dan Pop


Apr 22 '06 #3

P: n/a
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 Pfaff wrote:
"bobrics" <bo*****@gmail.com> writes:
/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?
--
"Am I missing something?"
--Dan Pop


Apr 22 '06 #4

P: n/a
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");
}





bobrics wrote:
Yes, I do have a main function in mycode.c file
Ben Pfaff wrote:
"bobrics" <bo*****@gmail.com> writes:
/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?
--
"Am I missing something?"
--Dan Pop


Apr 22 '06 #5

P: n/a
bobrics wrote:
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 Pfaff wrote:
"bobrics" <bo*****@gmail.com> writes:
/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?
--
"Am I missing something?"
--Dan Pop


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.
Apr 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.