Question

FYI: all work can be seen in the trac repo @ http://matthewh.me/scripts/browser/c/shared_library?order=name


The original post was to long. It essentially asked why my Makefiles were not finding any of the source when building from the root directory.


UPDATE: I did discover that the paths are all relative to the root directory, regardless of where the makefiles are placed.

I have the libs building now, however I am getting an undefined reference to main when compiling the app.

I will not paste the updated makefiles, but the source tree now looks like:

mehoggan@mehoggan-laptop:~/Code/shared_library$ make
gcc -c   -o ./c_lib/c_lib.o ./c_lib/c_lib.c
rm -f ./c_lib/libClib.so.1.0.0 ./c_lib/libClib.so ./c_lib/libClib.so.1 ./c_lib/libClib.so.1.0
gcc -m32 -Wl,-O1 -shared -o ./c_lib/libClib.so.1.0.0  -lpthread  
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so.1
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so.1.0
rm -f ./c_lib/libClib.a 
ar cqs ./c_lib/libClib.a ./c_lib/c_lib.o
cc -c -m32 -pipe -O2 -Wall -W -D_REENTRANT -I./c_lib/ -o ./app/main.o ./app/main.c
gcc -m32 -Wl,-O1 -o  ./app/main.o -L./c_lib/ -lClib
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [app/main] Error 1
mehoggan@mehoggan-laptop:~/Code/shared_library$ ls -lR
.:
total 12
drwxr-xr-x 2 mehoggan mehoggan 4096 2011-11-20 20:23 app
drwxr-xr-x 2 mehoggan mehoggan 4096 2011-11-20 20:23 c_lib
-rw-r--r-- 1 mehoggan mehoggan  824 2011-11-20 19:50 Makefile

./app:
total 8
-rw-r--r-- 1 mehoggan mehoggan 291 2011-11-20 18:52 main.c
-rw-r--r-- 1 mehoggan mehoggan 503 2011-11-20 20:23 Makefile

./c_lib:
total 28
-rw-r--r-- 1 mehoggan mehoggan  245 2011-11-20 18:54 c_lib.c
-rw-r--r-- 1 mehoggan mehoggan   46 2011-11-20 19:53 c_lib.h
-rw-r--r-- 1 mehoggan mehoggan  864 2011-11-20 20:23 c_lib.o
-rw-r--r-- 1 mehoggan mehoggan 1008 2011-11-20 20:23 libClib.a
lrwxrwxrwx 1 mehoggan mehoggan   18 2011-11-20 20:23 libClib.so -> ./libClib.so.1.0.0
lrwxrwxrwx 1 mehoggan mehoggan   18 2011-11-20 20:23 libClib.so.1 -> ./libClib.so.1.0.0
lrwxrwxrwx 1 mehoggan mehoggan   18 2011-11-20 20:23 libClib.so.1.0 -> ./libClib.so.1.0.0
-rwxr-xr-x 1 mehoggan mehoggan 6588 2011-11-20 20:23 libClib.so.1.0.0
-rw-r--r-- 1 mehoggan mehoggan  930 2011-11-20 20:20 Makefile

The contents of main.c are:

#include <stdio.h>
#include "c_lib.h"

int main(int argc, char *argv[])
{
    if (argc > 1) {
        char *arg = argv[1];
        printf("%s\n", arg);
    }

    char str[12] = "hello world\0";
    printf("%s\n", str);
    char *rev = reverse(str);
    printf("%s\n", rev);
    return 1;
}

Why can the compiler not find the main function?


UPDATE

Your fix helped get pass the undefined reference to main, however I am now getting another undefined reference:

[mehoggan@hogganz400 shared_library]$ make
gcc -c -m32 -pipe -O2 -Wall -W -D_REENTRANT -fPIC -I. -o ./c_lib/c_lib.o ./c_lib/c_lib.c
rm -f ./c_lib/libClib.so.1.0.0 ./c_lib/libClib.so ./c_lib/libClib.so.1 ./c_lib/libClib.so.1.0
gcc -m32 -Wl,-O1 -shared -o ./c_lib/libClib.so.1.0.0  -lpthread  
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so.1
ln -s ./libClib.so.1.0.0 ./c_lib/libClib.so.1.0
rm -f ./c_lib/libClib.a 
ar cqs ./c_lib/libClib.a ./c_lib/c_lib.o
gcc -c -m32 -pipe -O2 -Wall -W -D_REENTRANT -I./c_lib/ -o ./app/main.o ./app/main.c
gcc -m32 -Wl,-O1 -o ./app/main -L./c_lib/ -lClib ./app/main.o
./app/main.o: In function `main':
main.c:(.text+0x49): undefined reference to `reverse'
collect2: ld returned 1 exit status
make: *** [app/main] Error 1

Should there be a readable string in the .so file that indicates that reverse is in there?

[mehoggan@hogganz400 shared_library]$ strings ./c_lib/libClib.so.1
__gmon_start__
_init
_fini
__cxa_finalize
_Jv_RegisterClasses
libpthread.so.0
libc.so.6
_edata
__bss_start
_end
GLIBC_2.1.3
Was it helpful?

Solution

gcc -m32 -Wl,-O1 -o ./app/main.o -L./c_lib/ -lClib

You have an error in the variable setting the output file, and the next argument (main.o) is interpreted as the output instead. main.o is thus not loaded and not linked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top