1

I'm trying to create a static library in C. I'm use gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

My c files is here:

add.c //Addition operation perform sub.c // Subtract operation perform mul.c // Multiply operation perform div.c // Division operation perform 

And I'm trying to crate static library libabc.a using following command.

ar rcs libabc.a add.c sub.c mul.c div.c 

And It's successfully created. but when I run executable using ./a.out ./libabc.a, I got following error.

./libabc.a: error adding symbols: Archive has no index; run ranlib to add one collect2: error: ld returned 1 exit status 

What I doing wrong? please help me.

Thanks in Advance.

1 Answer 1

2

Use object files(.o) instead of source file(.c). Like,

ar rcs libabc.a sum.o mul.o add.o div.o 

Using -c option , create .o file.

cc -c -fpic add.c sub.c mul.c div.c 

To link purely statically library, use -static, Like

cc -static yourprgram.c libabc.a 

And run executable ./a.out. It successfully work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.