views:

144

answers:

4

Hello all! I'm not new in C++ although I'm new in Linux. I'm using CMake to precompile a cross platform game engine with some third party, but I have a lot of doubts about using libraries. My question is how to work with third party libraries. And where to put this libs. Apt installs libs in their official place (/usr/local, /usr/lib/ ..) but I develop in windows using local libs that are in a folder into my project dir.

Also, I need a good tutorial to know the rules of how libraries work. for example: when trying to compile my project, luabind is asking by liblua.s0.1, but AFAIK there is no way to generate this library with the source provided by lua (at least doing make, make install).

I know, this question is fuzzy but I haven't enough experience to be more concise.

update: After reading sombe answers, a more concise question is the following. If I install all third party libraries, how do I could distribute my program? How to manage dependecies without using a large readme?

Thanks for all*strong text*

+1  A: 

Okay, so this is one of the basic questions and while I myself might not come across very clear on this, here goes:

  1. While building a project, your compiler will need to find the header files of the libraries. The headers must be in the include path.
  2. after compilation is done, the linker will look for the library binaries (files.so or something like that). These must be in the Library path.

That's the basics.

If you have some specific libaries, you can add them to your own project-specific lib/ and include/ directories and add them to the include path and the library path respectively.

Adding these dirs to these paths can be done in many ways, depending upon how you are building the project. I'm sure there is something called LD_PATH involved in all this... But I don't really know the specifics involved with CMake.

A little googling can help you do the above with CMake.

Hope that helps,
jrh

Here Be Wolves
For me, to put libs in /usr/include is like add directories in global paths options in VC++. In all my programs, I put all third party libs into my project folder, then I configure the project (very easy in VC++). Later, if I need to distribute my project, only having a clean VC++ instalation and doing a repository checkout is enough to compile my project. At least in windows. Its just this what I'm looking for.
Killrazor
+1  A: 

If you are installing the libraries with a package manager, they will probably all end up in the right place. If not you can get the compiler to search for the by providing the an additional search path using the -L <path> flag. You should be able to pass this extra flag to CMake.

Incidentally the -I <path> can be used to add an extra directory to search for include files.

doron
+5  A: 

Where to put libraries

The best solution is to use your Linux distribution's packaging system (apt-get, yum, or similar) to install libraries from distro-provided packages wherever possible.

If the distro's packaged libraries aren't of a recent enough version, or if you need some nonstandard build options, or if you need a library that your distro doesn't provide, then you can build and install it yourself. You have two main options for where to put the library:

  • /usr/local (libraries under /usr/local/lib, headers under /usr/local/include). This installs the libraries systemwide and is probably the simplest solution, since you should then be able to build against them without taking any extra steps. Do NOT install libraries directly under /usr, since that will interfere with your distro's packaging system.
  • Under your project directory, as you did under Windows. This has the advantages of not requiring root access and not making systemwide changes, but you'll have to update your project's include paths and library paths, and you'll have to put any shared library files someplace where the dynamic linker can find them (using LD_LIBRARY_PATH or ld.so.conf - see the link for more details).

How libraries work

See David A. Wheeler's excellent Programming Library HOWTO. I'd recommend reading that then posting any specific questions as new topics.

How to distribute your program

Traditionally, Unix / Linux programs do not include copies of their dependencies. It's instead up to the end user or developer to install those dependencies themselves. This can require a "large README," as you said, but it has a few advantages:

  • Development libraries can be installed, managed, and updated via the distro's package manager, instead of each source copy having its own set of libraries to track.
  • There's only one copy of any given library on a system, so there's only one place that needs updating if, for example, a security flaw is found. (For example, consider the chaos that resulted when zlib, a very widely used compression library, was found to have a security flaw, so every application that included an affected version needed to be updated.)
  • If your program is popular enough (and is open source or at least freely available), then package maintainers for various Linux distributions may want to package it and include it in their distro. Package maintainers really don't like bundled libraries. See, for example, Fedora's page on the topic.

If you're distributing your program to end users, you may want to consider offering a package (.dpkg or .rpm) that they could simply download and install without having to use source. Ideally, from the end user's perspective, the package would be added to distros' repositories (if it's open source or at least freely available) so that users can download it using their package managers (apt-get or yum). This can all get complicated, because of the large number of Linux distros out there, but a Debian/Ubuntu compatible .dpkg and a Red Hat/CentOS/Fedora-compatible .rpm should cover a good percentage of end users. Building packages isn't too hard, and there are good howtos online.

Josh Kelley
I like `/opt` and stow
aaa
Great starting point!! I'm trying to do the same as in windows. I mean: if you have a clena VC++ instalation you can compile my project because all libraries are together. In linux I want the same. I'll read the how to. Thanks a lot!!!
Killrazor
For normal situations where it is used by everybody I agree /usr/local/X but when I am working with libraries that I am building and debugging (a lot) for myself (even if I am the only one using the system) I put them in ~/local/X
Martin York
A: 

for the first part of your question regarding Windows: there's no real standard place for libraries/headers on Windows, so the easy solution is: create your own. Simply provide a single lib/ and include/ on your system and have all your projects use it (by setting the path in a cmake file that you include everywhere). Put all third party libs in there, for example:

your projects:

d:/projects/projectA
d:/projects/projectB

third party stuff:

d:/api/lib/lua.lib
d:/api/include/lua/....

(you can even use symlinks aka 'directory junctions' if you have different version)

and the corresponding cmake file:

include_directories( d:/api/include )
link_directories( d:/api/lib )
stijn