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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值