Visual C++でのライブラリファイルを参照する方法について質問です。
使用しているのはVisual Studio 2013です。
以下のコードをライブラリとして作成しています。
MyDLL.cpp
C++
1#include "stdafx.h" 2#include "mydll.h" 3 4// 加算 5int add(int a, int b) { 6 return a + b; 7} 8 9// 減算 10int sub(int a, int b) { 11 return a - b; 12} 13 14// 乗算 15int multi(int a, int b) { 16 return a * b; 17} 18 19// 除算 20int divi(int a, int b) { 21 if (b == 0){ 22 return 0; 23 } 24 else{ 25 return a / b; 26 } 27}
mydll.h
C++
1extern "C" { 2 __declspec(dllexport) int add(int a, int b); 3 __declspec(dllexport) int* arr_a(int length); 4 __declspec(dllexport) void arr_b(int* arr, int length); 5}
上記のコードをコンパイルすることで、ライブラリファイルMyDLL.dll、MyDLL.lib
を作成します。
それに対して、参照先プロジェクトを以下のように設定します。
1 Visul studioで新規プロジェクトとして、「Win32コンソールアプリケーション」を追加。
2 ソリューションファイルと同じフォルダに、「lib」フォルダを追加。
3 libフォルダの下に、MyDLL.dll、MyDLL.libファイルをコピー。
4 ソリューションエクスプローラからプロジェクト名を右クリックし、プロパティを選択。
5 リンカ―→全般で、追加のライブラリディレクトリで「$(SolutionDir)lib」を設定。
6 参照元のDLLファイル「MyDLL.dll」を、参照先のReleaseフォルダ元にコピー。
7 参照先のプロジェクトで、以下のコードを作成。
UserCPlusDLL.cpp
C++
1#include "stdafx.h" 2#include "iostream" 3 4#pragma comment(lib, "MyDLL.lib") 5 6using namespace std; 7 8int _tmain(int argc, _TCHAR* argv[]) 9{ 10 int ans1; 11 int ans2; 12 int ans3; 13 int ans4; 14 15 ans1 = add(1, 2); 16 ans2 = sub(2, 1); 17 ans3 = multi(2, 2); 18 ans4 = divi(4, 2); 19 20 cout << "ans1=" << ans1 << "ans2=" << ans2 << "ans3=" << ans3 << "ans4=" << ans4 << endl; 21 22 return 0; 23}
上記のようにして参照先のプロジェクトでビルドを行うと、
IntelliSense: 識別子 "add" が定義されていません
error C3861: 'add': 識別子が見つかりませんでした
の類のエラーが発生します。
他の関数呼び出しに関しても同様です。
どうやら、ライブラリの参照が上手くいっていないようです。
どこか手順で誤っている個所、足りない箇所が分かる方がおられましたら、宜しくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/24 03:41 編集