奇怪的 C++/CLI 链接问题 - 无法解析的外部符号,但头文件已链接

发布于 2024-12-26 11:35:13 字数 1643 浏览 3 评论 0原文

我试图在 C++/CLI 中做一些非常简单的事情,但是我遇到了 Visual Studio 的问题,我不明白为什么。

我有两个项目,其中一个包含一个名为 JNIUtils.h 的头文件。它的内容非常简单 -

#pragma once

class JNIUtils
{
public:
    JNIUtils( void );
    ~JNIUtils( void );
};

而且它的实现也非常简单 -

#include "stdafx.h"

#include "JNIUtils.h"

JNIUtils::JNIUtils( void )
{

}

JNIUtils::~JNIUtils( void )
{

}

我想做的就是在另一个项目中创建这个新定义类型的对象。在包含此头文件的项目中,我转到

“项目属性”>“配置属性> VC++目录>包含目录

我添加了一个条目,其中包含我要包含的头文件所在的路径 (JNIUtils.h)

我试图在其中创建此对象的实例的 CPP 文件如下所示:

#include "stdafx.h"
#include "JNIUtils.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    JNIUtils *util = new JNIUtils();
    Console::WriteLine(L"Hello World");
    return 0;
}

当我尝试编译时,我得到以下错误:

Error   3   error LNK1120: 2 unresolved externals   C:\zcarter\UDK\WebSvcClients\Debug\JNITester.exe    JNITester
Error   2   error LNK2019: unresolved external symbol "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)   C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj    JNITester
Error   1   error LNK2028: unresolved token (0A000009) "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)  C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj    JNITester

任何人都可以告诉我我在这里做错了什么吗?

I'm trying to do something very simple in C++/CLI, however I'm running into issues with Visual Studio and I can't figure out why.

I have two projects, one which contains a header file named JNIUtils.h. Its content is very barebones -

#pragma once

class JNIUtils
{
public:
    JNIUtils( void );
    ~JNIUtils( void );
};

and its implementation is very simple as well -

#include "stdafx.h"

#include "JNIUtils.h"

JNIUtils::JNIUtils( void )
{

}

JNIUtils::~JNIUtils( void )
{

}

All I'm trying to do is to create an object of this newly defined type in another project. In the project where I'm including this header file, I've gone to

Project Properties > Configuration Properties > VC++ Directories > Include Directories

I added an entry with the path to where the header file I'm including resides (JNIUtils.h)

The CPP file I'm trying to create an instance of this object in looks as follows:

#include "stdafx.h"
#include "JNIUtils.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    JNIUtils *util = new JNIUtils();
    Console::WriteLine(L"Hello World");
    return 0;
}

When I try to compile I get the following errors:

Error   3   error LNK1120: 2 unresolved externals   C:\zcarter\UDK\WebSvcClients\Debug\JNITester.exe    JNITester
Error   2   error LNK2019: unresolved external symbol "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$HYMHP$01AP$AAVString@System@@@Z)   C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj    JNITester
Error   1   error LNK2028: unresolved token (0A000009) "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$HYMHP$01AP$AAVString@System@@@Z)  C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj    JNITester

Can anyone tell me what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2025-01-02 11:35:13

当您想要将一个项目中的类使用到另一个项目中时,您需要执行两件事:

1) 将库编译代码 (.lib) 链接到您的主应用程序代码。
如果你有vs2010,只需在Project->Properties->Common->References部分添加项目,所有的辛苦工作都会为你完成。

如果您使用的是vs2008或更低版本,则必须将项目添加到Project->Dependencies...中的依赖项中。然后,进入Project->Properties->Linker->Input并将.lib文件添加到现有的.dll列表中。使用相对路径,以便这可以在其他计算机上运行。请注意,vs2008 中还有一个 Reference 部分,但我相信它仅适用于 CLR 程序集。

2) 将标头添加到包含目录

您已经计算出了该部分,但我建议您将它们添加到Project->Properties->C++->Additional Include Directory< /code> 相反,因为您使用的方法将无法在另一台计算机上工作,除非他们进行与您相同的配置。

When you want to use classes from a project into another one, there are two things that you need to do:

1) Link the library compiled code (.lib) to your main application code.
If you have vs2010, simply add the project in the Project->Properties->Common->References section, all the hard work will be done for you.

If you have vs2008 or lower, you must add the project to the dependencies in Project->Dependencies.... Then, go in Project->Properties->Linker->Input and add the .lib file to the existing list of .dll. Use a relative path so that this will work on other computers. Note that there is also a Reference section in vs2008, but I believe it only works for CLR assemblies.

2) Add the headers to the include directories

You figured that part already, but I recommend you to add them to the Project->Properties->C++->Additional Include Directories instead, as the method you used will not work on another computer unless they make the same configuration as you.

假装不在乎 2025-01-02 11:35:13

问题的关键是您尝试在另一个项目中使用此对象。

为此,您必须将该类编译成类库(我建议在这里使用静态类库),然后在第二个程序的编译器设置中链接该库。这实际上有点乏味,但是非常值得尝试!

请注意,这里有两个单独的“链接”。代码中的一个,其中包含头文件;以及链接器中的一个(发生在编译器之后),您链接目标文件(现在位于静态库文件内)。

the key to your problem is that your trying to use this object in another project.

to do this, you must compile the class into a class library (i suggest a static one here), and then link this library in your compiler settings for the second program. this is somewhat tedious actually, but very good to try out!

note that there are two separate "linkings" here. one in your code, where you include the header file; and one in your linker (happens after compiler) where you link the object file (which is now inside a static library file).

漫雪独思 2025-01-02 11:35:13

在我的例子中,错误消息显示LNK2019 unresolved external symbol __imp_someFunctionName referenced in function ...,并且我发现函数名称的前缀__imp_意味着程序正在使用__declspec(dllimport),而不是__declspec(dllexport)

因此,我将正确的名称添加到 Project->Properties->Configuration Properties->C/C++->Preprocessor 中,让 __declspec(dllexport) 工作,然后可以解决错误并取得成功。

参考:如何我可以去掉 VC++ 链接器中的 __imp__ 前缀吗?

In my case the error message show LNK2019 unresolved external symbol __imp_someFunctionName referenced in function ..., and I find that the prefix __imp_ of function name, means that the program is using __declspec(dllimport), not __declspec(dllexport).

So I add the proper name to Project->Properties->Configuration Properties->C/C++->Preprocessor, let __declspec(dllexport) work, then can solve error and build success.

reference to: How can I get rid of the __imp__ prefix in the linker in VC++?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文