Skip to content
Advertisement

g++ custom dynamic library linking error undefined symbol

Ok, I’m looking for a solution for 2 days now. I didn’t find anything to solve my problems.

What is currently going on? So, I tried creating a dynamic library (.so) on Linux Mint Maya 13 with g++.

foolib.h:

#pragma once
#include <stdio.h>

void foo(
    void
    );

foolib.cpp:

#include "foolib.h"

void foo(
    void
    )
{
   printf ("Hello World!n");
};

main.cpp:

#include "foolib.h"

int main(
    int    argc,
    char** argv
    )
{
    foo ();
};

I compiled these files with these instructions:

libfoo.so:

g++ -shared -o libfoo.so -fpic foolib.cpp

foo:

g++ main.cpp -o foo -L -lfoo

Creating libfoo.so works without any errors, but foo throws undefined reference ´foo’. I copied sample code from several web pages and tried to compile it, always the same result.

The funny is, I can link do libdl.so (-ldl), load my .so and my function. What am I doing wrong?

I hope I could formulate my question correctly. Please tell me if I didn’t. : )

Advertisement

Answer

You should use:

g++ main.cpp -o foo -L./ -lfoo

or

g++ main.cpp -o foo libfoo.so
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement