[转]用g++编译动态链接库

 

  写了一个最简单的动态链接库程序,使用g++命令行编译。怕以后忘记,就把它记到blog中。
动态库导出头文件:
/**
  * file: dll.h
  * Powered by JGood 2009-09-22
  */

#ifndef __dll_h__
#define __dll_h__

#ifdef __MY_DLL_LIB__
    #define DLL_EXPORT extern "C" __declspec(dllexport)
#else
    #define DLL_EXPORT extern "C" __declspec(dllimport)
#endif

DLL_EXPORT int jmax(int x, int y);

#endif
动态库实现:
编译成obj文件:g++ -c -o dll.obj dll.cpp
链接obj,生成dll: g++ -shared -o dll.so dll.obj
/**
  * file: dll.cpp
  * Powered by JGood 2009-09-22
  */

#define __MY_DLL_LIB__
#include "dll.h"

int jmax(int x, int y)
{
    return x > y ? x : y;
}


调用动态库:
直接编译成exe: g++ main.cpp dll.so -o main.exe
/**
  * file: main.cpp
  * Powered by JGood 2009-09-22
  */

#include "dll.h"
#include <iostream>

using namespace std;

int main()
{
    int a = 20;
    int b = 40;
    cout << jmax(a, b) << endl;

    return 0;

转载于:https://www.cnblogs.com/foxhengxing/archive/2010/12/02/1894736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值