简体   繁体   中英

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

You should use:

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

or

g++ main.cpp -o foo libfoo.so

You state your foo compilation/link is with g++ main.cpp -o foo -L -lfoo and this is where the problem is. The -L option requires a parameter that gives the linker an additional directory to search for libraries but you have not provided it. So in your case, the linker thinks -lfoo is the name of a directory to search in, not a library to link in.

Change -L to -L. and it should work.

See this documentation for more information .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM