如何编译C和C++代码写的动态库

suyangwei 2011-05-17 03:01:59
动态库由file1.c和file2.cpp组成,如何编译动态库?
应用程序调用该动态库,如何编译应用程序
...全文
2461 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jernymy 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 suyangwei 的回复:]
引用 5 楼 jernymy 的回复:

C/C++ code

一、作为测试用,当前目录有这样几个文件

1. testc的模块的文件,生成so
testc.h
// jernymy 20110517 test c module(.h) for so linker
// testc.h

#ifndef _TESTC_H_
#define _TESTC_H_

// ……
[/Quote]

估计是环境有区别吧

测试程序的打包文件
http://download.csdn.net/source/3292101

gcc的版本是
#gcc -v
使用内建 specs。
目标:i486-linux-gnu
配置为:../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
线程模型:posix
gcc 版本 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
suyangwei 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jernymy 的回复:]

C/C++ code

一、作为测试用,当前目录有这样几个文件

1. testc的模块的文件,生成so
testc.h
// jernymy 20110517 test c module(.h) for so linker
// testc.h

#ifndef _TESTC_H_
#define _TESTC_H_

// here used extern "C"
#ifdef __cpl……
[/Quote]
将你的例子测试了一下,编译main.cpp时,提示TestC函数未定义。
jernymy 2011-05-17
  • 打赏
  • 举报
回复

一、作为测试用,当前目录有这样几个文件

1. testc的模块的文件,生成so
testc.h
// jernymy 20110517 test c module(.h) for so linker
// testc.h

#ifndef _TESTC_H_
#define _TESTC_H_

// here used extern "C"
#ifdef __cplusplus
extern "C" {
#endif

int TestC(void);

#ifdef __cplusplus
}
#endif

#endif

----------------------------------------------------------------
testc.c
// jernymy 20110517 test c module(.c) for so linker
// testc.c

#include "stdio.h"

int TestC(void)
{
printf("from file %s, line: %d\n", __FILE__, __LINE__);
return 0;
}


2. testcpp的模块的文件,生成so

testcpp.h
// jernymy 20110517 test cpp module(.h) for so linker
// testcpp.h

#ifndef _TESTCPP_H_
#define _TESTCPP_H_

int TestCPP(void);

#endif

----------------------------------------------------------------
testcpp.cpp
// jernymy 20110517 test cpp module(.cpp) for so linker
// testcpp.cpp

#include <iostream>
using namespace std;

int TestCPP(void)
{
cout<<"from file "<<__FILE__<<", line: "<<__LINE__<<endl;
return 0;
}


3. main文件,生成可执行文件

main.cpp
// jernymy 20110517 main cpp module(.cpp) for exec
// main.cpp

#include "testc.h" // testc module
#include "testcpp.h" // testcpp module

int main(void)
{
TestC(); // call .c method
TestCPP(); // call .cpp method
}

二、通过ubuntu的gcc编译testc.c, testcpp.cpp生成libtest.so。
main.cpp生成mtest
jernymy@jernymy-desktop$ gcc -o libtest.so testc.c testcpp.cpp -shared -lstdc++
jernymy@jernymy-desktop$
jernymy@jernymy-desktop$ gcc -o mtest -I ./ main.cpp -L . -ltest -lstdc++


三、执行mtest,发现无法找到libtest.so。
这时通过ldd查看mtest依附的lib的情况,发现libtest.so => not found,
需要将当前(生成的libtest.so)目录加入到lib的so搜索路径
jernymy@jernymy-desktop$
jernymy@jernymy-deskto$ ./mtest
./mtest: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
jernymy@jernymy-deskto$ ldd ./mtest
linux-gate.so.1 => (0xb8035000)
libtest.so => not found
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7f37000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7dd3000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7dad000)
/lib/ld-linux.so.2 (0xb8036000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7d9e000)

jernymy@jernymy-desktop$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./
jernymy@jernymy-desktop$ ./mtest
from file testc.c, line: 5
from file testcpp.cpp, line: 7
jernymy@jernymy-desktop$


接下来,您也可以编写这样的例子了。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jernymy/archive/2011/05/17/6428210.aspx
suyangwei 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 justkk 的回复:]

在.cpp文件中注意使用extern "C" {} 包含函数
[/Quote]
这个好像解决不了问题。网上看到这个帖子http://bbs.chinaunix.net/viewthread.php?tid=970320&extra=&page=1
我现在是和他帖子一样的问题。
justkk 2011-05-17
  • 打赏
  • 举报
回复
在.cpp文件中注意使用extern "C" {} 包含函数
goodmrning 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuimo 的回复:]
g++ -fPIC -shared -o outFile.so file1.cpp file2.cpp编译成动态链接库
编译工程时,将该库导入即可: 若库在当前目录下,可直接使用 如./outFile.so
[/Quote]

顶一个!
yuimo 2011-05-17
  • 打赏
  • 举报
回复
g++ -fPIC -shared -o outFile.so file1.cpp file2.cpp编译成动态链接库
编译工程时,将该库导入即可: 若库在当前目录下,可直接使用 如./outFile.so

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧