C++/CLI 代码的基本设置,以便它可以被非托管 C/++ 使用
basic setup of C++/CLI code so that it can be consumed by unmanaged C/++
我正在尝试学习 C++/CLI,计划编写一个将由(非托管)C 代码使用的 DLL。但是,我无法构建最基本的示例,如下所示:
我在 Visual Studio Express 2013 工作。
创建新项目 -> CLR ->class 库
LearnCli.h:
extern "C" __declspec(dllexport)
int __stdcall TestFunc();
LearnCli.cpp:
#include "stdafx.h"
#include "LearnCli.h"
int __stdcall TestFunc()
{
return 3;
}
构建没有问题。
添加项目 -> Win32 -> 控制台应用程序
从新控制台项目的解决方案资源管理器的上下文菜单中:
添加 -> 参考 -> LearnCli
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include "..\LearnCli\LearnCli.h"
ConsoleApplication.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int z;
z=TestFunc();
cout << "Function returns:" << z << endl;
cin.get();
return 0;
}
intellisense 没有问题,但在构建时:
Error 1 error LNK2019: unresolved external symbol _TestFunc@0 referenced in function _wmain [path]\Projects\LearnCli\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
我错过了什么不允许 win32 控制台应用程序找到该功能?干杯。
编辑
感谢评论和 link,我已将 LearnCli.h 文件更改为
#ifdef LEARNCLIAPI_EXPORTS
#define LearnCliApi_DECLSPEC __declspec(dllexport)
#else
#define LearnCliApi_DECLSPEC __declspec(dllimport)
#endif
然后转到项目 -> 属性 -> C/C++ -> 预处理器 -> 定义
并添加了 LEARNCLIAPI_EXPORTS。不幸的是错误没有改变
您需要 link 您的应用程序 (exe) 项目使用从 dll 项目构建的 .lib。
您可以从“项目设置”>>“链接器”>>“输入文件”中添加它,或者简单地在您的源代码中添加一行。
即
pragma(comment, "lib:<your_lib.lib>")
我正在尝试学习 C++/CLI,计划编写一个将由(非托管)C 代码使用的 DLL。但是,我无法构建最基本的示例,如下所示:
我在 Visual Studio Express 2013 工作。
创建新项目 -> CLR ->class 库
LearnCli.h:
extern "C" __declspec(dllexport)
int __stdcall TestFunc();
LearnCli.cpp:
#include "stdafx.h"
#include "LearnCli.h"
int __stdcall TestFunc()
{
return 3;
}
构建没有问题。
添加项目 -> Win32 -> 控制台应用程序
从新控制台项目的解决方案资源管理器的上下文菜单中:
添加 -> 参考 -> LearnCli
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include "..\LearnCli\LearnCli.h"
ConsoleApplication.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int z;
z=TestFunc();
cout << "Function returns:" << z << endl;
cin.get();
return 0;
}
intellisense 没有问题,但在构建时:
Error 1 error LNK2019: unresolved external symbol _TestFunc@0 referenced in function _wmain [path]\Projects\LearnCli\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
我错过了什么不允许 win32 控制台应用程序找到该功能?干杯。
编辑
感谢评论和 link,我已将 LearnCli.h 文件更改为
#ifdef LEARNCLIAPI_EXPORTS
#define LearnCliApi_DECLSPEC __declspec(dllexport)
#else
#define LearnCliApi_DECLSPEC __declspec(dllimport)
#endif
然后转到项目 -> 属性 -> C/C++ -> 预处理器 -> 定义 并添加了 LEARNCLIAPI_EXPORTS。不幸的是错误没有改变
您需要 link 您的应用程序 (exe) 项目使用从 dll 项目构建的 .lib。
您可以从“项目设置”>>“链接器”>>“输入文件”中添加它,或者简单地在您的源代码中添加一行。
即
pragma(comment, "lib:<your_lib.lib>")