Groups
Groups
Sign in
Groups
Groups
LinkU
Conversations
About
Send feedback
Help
gcc编译dll文件
9 views
Skip to first unread message
呢斯特拉可
unread,
May 27, 2009, 8:43:48 AM
5/27/09
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to LinkU
// "SayHello.h"
class SayHello{
public:
void say();
void sayhi();
};
//SayHello.cpp
#include "SayHello.h"
#include <iostream>
using namespace std;
void SayHello::say(){
cout<<"hello"<<endl;
}
//SayOther.cpp
#include "SayHello.h"
#include <iostream>
using namespace std;
void SayHello::sayhi(){
cout<<"hi"<<endl;
}
//Main.cpp
#include "SayHello.h"
int main(){
SayHello* sayHello=new SayHello();
sayHello->say();
sayHello->sayhi();
return 0;
}
创建静态库
用 -c 参数生成目标文件但不链接
g++ -c SayHello.cpp g++ -c SayOther.cpp
这样各生成一个 .o 的文件
打成静态库名为 libHello.a
ar -r libHello.a SayHello.o SayOther.o
这样当 Main.cpp 用到库时就这样编译
g++ Main.cpp libHello.a -o Main 就会生成名子为 Main 的可执行文件
创建动态库
同样先生成目标文件不链接
g++ -c -fpic SayOther.cpp
g++ -c -fpic SayHello.cpp
-fpic 的意思是位置独立代码 ,指示编译程序生成的代码要适合共享库的内容这样的代码能够根据载入内存的位置计算内部地址
然后打包 生成动态库
g++ -shared SayHello.o SayOther.o -o libHello.so
当有程序用到它时就这样编译
g++ Main.cpp libHello.so -o Main
为了运行程序必须安装共享库以保证程序运行时可以找到该共享库
可以将库放在 /usr/lib 或 /lib下才能运行程序
Reply all
Reply to author
Forward
0 new messages