Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22310

Different behavior of undefined reference error on linux gcc during linking with object file vs static library

$
0
0

I have following two source codes and want to link them.

// test.c#include <stdio.h>void lib2();void lib1(){    lib2();    return 0;}
// main.c#include <stdio.h>int main() {    return 0;}

I've used gcc -c main.c and gcc -c test.c to generate objects files

$ ls *.omain.o  test.o

and I've used ar rcs test.a test.o command to generate static library(test.a) from object file test.o

Then, I tried to build executable by linking main.o with test.a or test.o. As far as I know, a static library file(.a extension) is a kind of simple collection of object files(.o). so I expected both would give same result: error or success. but it didn't.

Linking with the object file gives undefined reference error.

$ gcc -o main main.o test.o/usr/bin/ld: test.o: in function `lib1':test.c:(.text+0xe): undefined reference to `lib2'collect2: error: ld returned 1 exit status$

but linking with the static library doesn't give any error and success on compilation.

$ gcc -o main main.o test.a$

Why is this happening? and how can I get undefined reference errors even when linking with static libraries?


Viewing all articles
Browse latest Browse all 22310

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>