0

I would like to create a dynamic library for c++ program on linux. In c++ program/system I`m using libconfig++ library, libpqxx library, some boost and c++11.

My steps: 1)

g++ -Wall -I/usr/local/include/ -std=c++0x -lconfig++ -Wall -lpqxx -lpq -fPIC -c ../SourceFiles/DBHandler.cpp ../SourceFiles/ParamServer.cpp ../SourceFiles/Functions.cpp

2)

g++ -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o

3)

ln -sf libctest.so.1.0 libctest.so.1

4)

ln -sf libctest.so.1.0 libctest.so

5) compile

g++ -Wall -I/path/to/include-files -L/path/to/libraries program.cpp -I/usr/local/include/ -std=c++0x -lconfig++ -lpqxx -lpq -lctest -o prog

After execute above command :

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

What am I doing wrong?

Here is the reference: enter link description here

andrew
  • 3,083
  • 4
  • 24
  • 29

2 Answers2

7

In step 5, you forgot -L. to look for libraries in the current directory.

By default, only a [long] list of system directories is used when searching for libraries.

You will also need to add . to the LD_LIBRARY_PATH environment variable before executing your program, so that the current directory is searched at runtime, too. Running ldconfig will avoid this, but if you are only testing your library and do not want to persistently affect your system, I would stick to the LD_LIBRARY_PATH approach.

An alternative is to "install" your library into one of those directories, such as /usr/local/lib (or your equivalent). You should use ldconfig after doing this, so that the dynamic library cache and all your symlinks are set up for you. This is the canonical approach but may not be suitable during iterative development of said library.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • One also needs to run `ldconfig`, so this answer alone won't do. – Dirk Eddelbuettel Feb 24 '14 at 23:06
  • @DirkEddelbuettel: No, not necessarily. – Lightness Races in Orbit Feb 24 '14 at 23:06
  • Disagree. Allow me to quote from the link I added to my answer: _Once you've created a shared library, you'll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8)._ Note the 'and'; this is not optional. – Dirk Eddelbuettel Feb 24 '14 at 23:07
  • 1
    @DirkEddelbuettel: That's great when you want to install it. But that is not always the case. How I develop and test my libraries is most certainly "optional": it's not up to you or a FAQ to dictate that! – Lightness Races in Orbit Feb 24 '14 at 23:08
  • You. Can't. Run. The. Binary. Otherwise. Let;s not discuss `rpath` and friends, OP had a clear question. – Dirk Eddelbuettel Feb 24 '14 at 23:08
  • 3
    @DirkEddelbuettel: I've been doing so quite happily throughout my career, and I stand by the advice detailed in my answer, for the use case I also detailed in my answer. No. Need. To. Speak. Like. This. Just. Because. You. Disagree. – Lightness Races in Orbit Feb 24 '14 at 23:08
  • Hmm, when I executed: g++ -Wall -I/path/to/include-files -L. program.cpp -I/usr/local/include/ -std=c++0x -lconfig++ -lpqxx -lpq -lctest -o prog I`ve got errors like: ./libctest.so: undefined reference to `pqxx::result::columns() const' ./libctest.so: undefined reference to `pqxx::connection_base::port()' ...and much more because as I wrote in the question I am using libpqxx for postgreSQL support. When I execute makefile normally without dynamic library everything is compiling and running properly. – andrew Feb 24 '14 at 23:22
  • 1
    @andrew: That's a whole new problem. :) To which the answer is almost certainly down to the order of your `-l` flags on the command line... Try putting `-lctest` _before_ the flags for third-party libraries. Dependencies are resolved left-to-right (more or less) – Lightness Races in Orbit Feb 24 '14 at 23:24
  • @andrew In other words, put `-lctest` before `-lpqxx`. – Casey Feb 24 '14 at 23:26
  • It`s ok: g++ -Wall -I/path/to/include-files -L. program.cpp -I/usr/local/include/ -lctest -std=c++0x -lconfig++ -lpqxx -lpq -o prog Compile without errors. Maybe this is because of point number 4? Next : ./prog After that: ./prog: error while loading shared libraries: libctest.so.1: cannot open shared object file: No such file or directory I don`t know why? In directory there is libctest.so.1 file. – andrew Feb 24 '14 at 23:35
  • 1
    @andrew: That's because you didn't follow my instructions. You could run `LD_LIBRARY_PATH=. ./prog`. Dirk certainly isn't wrong in that there are other ways to go about all this, `ldconfig` and `rpath` both being good examples: it's up to you to decide which approach you take. In my answer I have applied my own experience and tried to guess at your use case, which is a temporary library file located in a development directory for testing. – Lightness Races in Orbit Feb 24 '14 at 23:42
  • You could also learn a lot more about shared librairies in the guide from Ulrich Drepper : http://www.akkadia.org/drepper/dsohowto.pdf – pan- Feb 25 '14 at 00:14
2

You need to ldconfig update the dynamic library cache -- it will also create the symbolic links for you.

See eg Section 3.5 of this Linux Documentation Project HOWTO

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725