Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: compile some objects (for a shared library) without main() fails?

From what I have learned I need to follow this step to prepare for making a shared library:

gcc -fPIC libfoo.c -o libfoo.o

And then I link it. I had tried making a makefile to aid in these steps, but there appears to be errors happening now.

This occurs when I run the make file:

foo@box:~/Projects/so$ gcc -fPIC ./libfoo.c -o libfoo.o
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

How can I compile the library file without the main function, as it is not a program and is intended to be a library?

If it helps my program is basically this (interpreted)

(stdio and openssl headers here)

(debugging macro definitions here)

(two functions, gettime() and opensslrandom() defined here)

I seem to have problems understanding about the macros as well, as they would be in the shared library in the end they are useless in the shared library? I included them in libfoo.h to be included, although I have yet to see if the macros work.

like image 896
Tim N. Avatar asked Mar 27 '11 03:03

Tim N.


People also ask

Which of the following options is necessary to create a shared library?

The -shared or -dynamiclib option is required to create a shared library.

What is a shared object in C?

A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.

How do I add a shared library in Makefile?

LIBS = -L$(LIB) -lfuse -lsqlite3 -lkw_taglib -ltag_c -ltag -Wl,-rpath=. So the binary will search in the current directory for dyn-libraries. However, you add ../lib to your LD_LIBRARY_PATH later, for execution of the binary, so the given path .


1 Answers

You need -c

gcc -fPIC -c libfoo.c 

for generating the object files.

You may want to look at: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

like image 200
Brian Roach Avatar answered Nov 03 '22 03:11

Brian Roach