embedded
embedded

Reputation: 11

Create a static libary and link against it

Hello beautiful people,

i'm trying to create a static libary and to compile against it. I've allready created a small static libary and a header for it.

Header (math.h):

int add (int a, int b);
int sub (int a, int b);

add.c:

int add (int a, int b) { return a + b; }

sub.c:

int sub (int a, int b) { return a - b; }

I've created my static libary with the following commands:

gcc -c add.c

gcc -c sub.c

ar rcs libmymath.a add.o sub.o

Now my main.c

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

int main( int argc, char **argv ) {
    printf("Result : %d\n", add(5,7) );
    return 0;
}

I can compile it with the following command:

gcc main.c libmymath.a -o main

But if i compile it the following way, it fails.

gcc main.c -lmymath -L. -o main

It fails with the following error:

/usr/bin/ld: cannot find -lmymath collect2:

error: ld returned 1 exit status

even a change to

gcc main.c -llibmymath -L. -o main

fails and even if i include the header mymath.h to gcc Can you help me ?

Upvotes: 0

Views: 42

Answers (1)

Loc Tran
Loc Tran

Reputation: 1180

gcc understood -lmymath by libmath.so or libmath.a already. So when you add lib word in -llibmymath. This case the gcc understood your library name being liblibmymath.a. So, please replace this command

gcc main.c -llibmymath -L. -o main

by

gcc main.c  -o main -L. -lmymath 

It should work.

Upvotes: 1

Related Questions