Eclipse C/C++ and Linux Libraries

This is a really quick tutorial on configuring Eclipse CDT (C/C++ Development Tools) so you can work with shared libraries.  A lot of this may be basic for some readers, but for a programmer that’s used to Visual Studio and a Windows dll environment it may not.  Windows uses DLL (Dynamic link library) with the .dll extension.  The closest linux equivalent is the shared library which uses a .so extension.   I’m going to use libavcodec.so as the shared library to be used in my example. This library is part of the ffmpeg project.

Assuming you already have Eclipse CDT, the first thing you need to do is create a new project.  After starting Eclipse, click on File->New->C Project and you’ll get a dialog box to choose the type of project (executable, static library, shared library, etc).  For this example, I’m choosing Executable and Hello World ANSI C Project.  Put in a name and click Finish.

01-lec-new

Next you’ll see you have a new project in the left-side panel with a .c file containing a simple hello world program.

02-lec-project

Now in my example I’ll add the line #include <avcodec.h> to set up my include.  If you need to add an include directory (where your header files are located) then in the Eclipse menu go to Project->Properties then open the C/C++ Build branch and click on Settings.  On the right side click on the Directories branch of GCC C Compiler.  You can then add directories to your include path.

03-lec-include

In this same dialog you can click on Libraries in the GCC C Linker branch to add any libraries to the project for linking.  The library I’m adding is the libavcodec.so shared library.  To add this (notice in the screenshot) I only need to add avcodec, without the prefix lib and .so extension.  This might be new to Windows programmers.  Since you’re adding a library it’s assumed to have the lib and .so in the name.

04-lec-libs

Now let’s just add a couple of lines to the main() so we can test a compile with the libavcodec library.

#include <stdio.h>
#include <stdlib.h>
#include <avcodec.h>
int main(void) {
   avcodec_init();
   avcodec_register_all();
   return 1;
}

And then to run it, in the menu toolbar click on the run arrow and choose Run As->C/C++ Local Application.
05-lec-run
Getting no errors means success.


23 Comments

  1. superbumbot says:

    Very good. As a experienced Java developer but newbie in C++ this articles is what we all need to have in our bookmarks.

    I’ll link to it in my blog.

    Gràcies / Gracias / Thanks / Takk

  2. [...] [upmod] [downmod] /home/nick » Blog Archive » Eclipse C/C++ and Linux Libraries – Discussions on Programming, Linux,… (whatwouldnickdo.com) 1 points posted 3 months, 1 week ago by SixSixSix tags c imported c++ [...]

  3. pjimenez says:

    Very good tutorial, thanks.

  4. kevin says:

    Thanks a lot!!!!
    Every body else is Just hell bent to help u in windows, but no body helped in linux alas you!!
    Thanks again….

  5. steve says:

    Exactly what I was looking for.

    Thanks a lot!

  6. radovan says:

    Thank you very much!
    I was fighting with linker somethink about hour.

  7. newtux says:

    Thank you very much! I was trying to link a shared library libMySharedLib.so and eclipse was complaining that it could not find it. After reading your example I entered only MySharedLib without lib and .so in the settings and now it works.

  8. Nick says:

    np newtux. Sometimes the smallest things are left out of the official docs for us to figure out. Glad this helped you out.

  9. Was ben says:

    [...] Eclipse C/C++ and Linux Libraries | /home/nick EclipseProject – Eclipse User Community Forums-viewtopic-Probleme mit der Einrichtung von CDT Ich [...]

  10. manish says:

    I am new to using Eclipse for C++ development. If I want to use the .so and header files (downloaded and compiled for a particular library say boost or avcodec) located in my home directory instead of using the .so and header files located in system directory (say /usr/ or /usr/local), what do we configure that in eclipse? Any suggestion?

  11. Mark says:

    Thank you, thank you, thank you! I’m a .Netter and have been pulling my hair out on this one. Nothing I could find in the docs and on the net until your article here. Much appreciated!

  12. Keith says:

    Dude, thanks a ton!!! This is an awesome tutorial!

  13. mike says:

    thanks for the reminder about removing lib*.so from library names.

  14. Bertrand says:

    Your post is quite straightforward but i’ve been stuck for days by this simple prefix removing thing.
    thank’s a lot.

  15. Dave says:

    Thanks…I am a java developer and new to the c environment so this really helped me a lot

  16. Nilay Badavne says:

    I was breaking my head over getting my project working with static libraries!

    Your tips helped a lot!

    Many thanks!

    ~Nilay

  17. Jason says:

    If I want eclipse to build using the static libraries, I understand that I use the same process and do not include the lib or .a in the name, but how to I tell eclipse to use the static library instead of the shared library?

    Thanks.

  18. Morris says:

    Perfect! It’s just what I was looking for!

    Thank you!

  19. ashik says:

    how to give link to the library in ECLIPSE
    am getting error like
    /usr/bin/ld: cannot find -l-l
    collect2: ld returned 1 exit status

  20. Allan says:

    Thank you for sharing it. It really helped.

    If the lib is in the current directory you can include the lib search path.

  21. seraph says:

    Thanks a lot, made my day. I’ve been fighting to resolve this the whole day. Thanks again.

  22. Jeff says:

    THANKS!

    Also two hints for fellow newbies
    1: any paths that are added to the project (for includes or libraries) may be absolute or relative and the relative ones are, of course, relative to the location of the makefile.
    2: I found that my sql lite lib was libsqlite3.so.0 and that trailing .0 screwed me up. So I had to create a new link without the .0. I left the original one in place figuring someone may have needed it.

    THANKS!

  23. Squall says:

    I can’t find C/C++ Build in the Properties. Can it be hidden/disabled? if so, how can I enable it back?

    And for compiling using a makefile, does CDT generate the makefiles automatically?

Leave a Reply