user948620
user948620

Reputation:

undefined reference to `test' when linking shared object

I'm studying a tutorial how to link shared objects in C

Here's my make file

test: glenn.c libhala.so
    gcc glenn.c -L. -o test

libhala.so: hala.o
    gcc -shared hala.o -o libhala.so

hala.o: hala.c hala.h
    gcc -c -Wall -Werror -fpic hala.c

clean:
    rm *.o
    rm *.so
    rm test

hala.h

#ifndef HALA
#define HALA

extern void test(char*);
#endif

hala.c

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

extern void test(char* s)
{

    printf("%s", s);
}

glenn.c

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

int main()
{
    test("Hello There!");
    return 0;
}

This stocks me up. Help me please..

Upvotes: 2

Views: 446

Answers (2)

Rohan
Rohan

Reputation: 53386

Add -lhala while compiling glenn.c, so update makefile as

test: glenn.c libhala.so
    gcc glenn.c -L. -lhala -o test

Upvotes: 1

Haocheng
Haocheng

Reputation: 1343

You should add -lhaha when you link glenn.c.

gcc glenn.c -L. -lhala -o test

Upvotes: 2

Related Questions