Linker can't find shared library compiled by g++

21 views Asked by At

I have the following files:

funcs.hpp:

#include "iostream"

void func(void);

funcs.cpp:

#include "iostream"
#include "funcs.hpp"

void func(void)
{
        std::cout<< "Running func" << std::endl;
};     

main.cpp:

#include "iostream"

extern void func(void);


int main(void)
{
        std::cout<< "call func" << std::endl;
        func();
        return 1;
}

I compiled funcs.cpp into shared library via command:

g++ -fPIC -shared funcs.cpp -o libfuncs.so

Then I call

g++ main.cpp -L. -llibfuncs -o main.o

To link the shared library to main and compile but I get an error:

/usr/bin/ld: cannot find -llibfuncs: No such file or directory
collect2: error: ld returned 1 exit status

I follow the examples I saw on line exactly, I don't know what's going on.

1

There are 1 answers

1
FourierFlux On

So the issue was gcc assumes lib will be added to the library name, you need to remove the lib portion, the correct command is:

g++ main.cpp -L. -lfuncs -o main.o

I think this is poorly documented.