Nils Jonsson Nils Jonsson - 1 year ago 48
Linux Question

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++.


#pragma once
#include <stdio.h>

void foo(


#include "foolib.h"

void foo(
printf ("Hello World!\n");


#include "foolib.h"

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

I compiled these files with these instructions:

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


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

Creating 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 (-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. : )

Answer Source

You should use:

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


g++ main.cpp -o foo