Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't g++ link with the dynamic library I create?

I've been trying to make some applications which all rely on the same library, and dynamic libraries were my first thought: So I began writing the "Library":

/* ThinFS.h */

class FileSystem {
public:
    static void create_container(string file_name); //Creates a new container 
};

/* ThinFS.cpp */
#include "ThinFS.h"
void FileSystem::create_container(string file_name) {
     cout<<"Seems like I am going to create a new file called "<<file_name.c_str()<<endl;
}

I then compile the "Library"

g++ -shared -fPIC FileSystem.cpp -o ThinFS.o

I then quickly wrote a file that uses the Library:

#include "ThinFS.h"
int main() {
    FileSystem::create_container("foo");
    return (42);
}

I then tried to compile that with

g++ main.cpp -L. -lThinFS

But it won't compile with the following error:

/usr/bin/ld: cannot find -lThinFS
collect2: ld returned 1 exit status

I think I'm missing something very obvious, please help me :)

like image 522
lazlow Avatar asked Jan 04 '10 18:01

lazlow


2 Answers

-lfoo looks for a library called libfoo.a (static) or libfoo.so (shared) in the current library path, so to create the library, you need to use g++ -shared -fPIC FileSystem.cpp -o libThinFS.so

like image 190
Chris Dodd Avatar answered Sep 20 '22 09:09

Chris Dodd


You can use

g++ main.cpp -L. -l:ThinFS 

The use of "colon" will use the library name as it is, rather requiring a prefix of "lib"

like image 38
jernkuan Avatar answered Sep 22 '22 09:09

jernkuan