Eclipse C/C++ and Linux Libraries 加載動態庫.so

http://whatwouldnickdo.com/wordpress/328/eclipse-cdt-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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章