LoginSignup
2
1

More than 1 year has passed since last update.

C++関数エクスポート(DEFモジュール定義ファイル編)

Last updated at Posted at 2022-06-18

前書き

前は、下記記事でdllexportを中心にDLL関数のエクスポートについてメモしました。

dllexportとdllimportの使い方

C++関数のエクスポート(dllexport + extern "C" 編)

この記事で、dllexportと異なる方法をメモします。

モジュール定義(DEF)ファイル

下の図のように、Visual Studioでモジュール定義ファイルをプロジェクトに追加することができます。
image.png

このファイルに、
[関数名] @[番号]
のように記載すれば、関数がエクスポートされます。

source.def
LIBRARY
EXPORTS
output	@1

※「@」の後ろにある番号は、以下の事象と関連しています。

「序数 X がダイナミックライブラリから見つかりませんでした」というエラーの原因

DEFファイルが作成されてからコンパイルすれば、DLLと同名のlibファイルが作成され、DLLファイルにエクスポート情報が追記されます。
※確認方法は下記記事のDUMPBINの部分を参考

C++関数のエクスポート(dllexport + extern "C" 編)

モジュール定義ファイルのメリット

C++関数のエクスポート(dllexport + extern "C" 編)

上記記事に記載されている「Hello.h」ファイルと比べて、dllexportやextern "C"などの記載は不要になります。

ファイル数が1個増えましたが、ソースの可読性が向上します。
(特にWindows仕様があまり好きじゃない人に対して)

Hello.h
#pragma once

#include<stdio.h>

//①defファイルを使うとdllexportは不要
//#ifdef CPPDLL_EXPORTS
//#define DECLSPEC __declspec(dllexport)
//#else
//#define DECLSPEC __declspec(dllimport)
//#endif

typedef struct {
	int id;
	char name[20];
}st_info;

//②defファイルを使うと、extern "C"の指定も不要
//extern "C" {
//	DECLSPEC int output(st_info*);
//}

//③単純に関数を宣言するだけでよい
int output(st_info*);
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1