即使正确完成了 Visual Studio 项目的设置,也出现未解决的外部符号错误

发布于 2025-01-13 19:24:03 字数 3674 浏览 3 评论 0原文

简而言之,

我尝试使用现有的 C++ 库。 问题是它没有编译。 我获取了 src 代码(一个 Visual Studio 项目)并尝试编译它。 问题是我无法将我的项目链接到库。

更具体的细节是:

我已经完成的操作如下:

  1. 我打开了 .sln 文件并添加了另一个名为“Sampler”的子项目
  2. 我下载了库 src 代码依赖的所有 Microsoft 附加包。
  3. 我将该库编译到 Windows 32 平台 x86(使用 Visual Studio 2022)。编译成功。
  4. 我在.sln文件的同一目录中创建了一个目录Dependencies\OPCClientToolKitDependencies\OPCClientToolKit包含两个目录includelib
  5. 我将 .lib 放入 Dependencies\OPCClientToolKit\lib - 编译后的输出。
  6. 我将sdk库的src代码的.h文件放入Dependency\OPCClientToolKit\include中。
  7. 我通过 Sampler Project > 添加了依赖 .h 文件属性> C++ 通用。请参阅下面的图片。
  8. 我通过 Sampler Project > 添加了一个依赖文件夹 Dependencies\OPCClientToolKit\lib属性>链接器将军。请参阅下面的图片。
  9. 我通过 Sampler Project > 添加了依赖项 OPCClientToolKit.lib属性>链接器输入。请参阅下面的图片。

据我所知,我的所有步骤似乎都是有效且良好的。

但是,当我编译(通过 Visual Studio 点击“构建”)Sampler 项目代码(请参阅下面的代码)时,我收到一条错误消息:

错误消息:

1>------ Build started: Project: Sampler, Configuration: Release Win32 ------
1>Sampler.cpp
1>Sampler.obj : error LNK2001: unresolved external symbol "public: static class COPCHost * __cdecl COPCClient::makeHost(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?makeHost@COPCClient@@SAPAVCOPCHost@@ABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z)
1>C:\Users\Mark\Desktop\Projects\PID\OPC_DA\Release\Sampler.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Sampler.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请向我解释应该如何解决此问题。我只想包含并使用这个库,而不是修改它。

Dependecies\OPCClientToolKit\lib

Dependecies\OPCClientToolKit\include

在此处输入图像描述

采样器项目>属性> C++>一般

enter此处的图像描述

采样器项目>属性>链接器>一般

enter此处的图像描述

采样器项目>属性>链接器>输入

我正在使用这个库(OPC客户端sdk):

https://sourceforge.net/projects/opcclient/

互联网上其他人制作的使用示例:

https://github.com/Tibalt/OPC_DA/blob/master/OPCClientDemo/OPCClientDemo.cpp

我的代码只使用该库的函数和对象。

详细来说,只是尝试初始化与本地主机上的服务器的 opc 连接:

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <sys\timeb.h>
#include "opcda.h"
#include "OPCClient.h"
#include "OPCHost.h"
#include "OPCServer.h"
#include "OPCGroup.h"
#include "OPCItem.h"
using namespace std;

int main(void) {
    COPCClient::init();
    cout << "Done Init";
    cout << endl;
    CString hostName = "localhost";
    COPCHost* host = COPCClient::makeHost(hostName);
}

In a nutshell

I am tring to use an existing C++ library.
The problem is that it's not compiled.
I took the src code (a visual studio project) and tried to compile it.
The thing is that I can't link my project to the library.

More specific details are:

What I have already done is the following:

  1. I opened the .sln file and added another sub-project called "Sampler"
  2. I downloaded all Microsoft additional packages that the library src code depends on.
  3. I compiled the library to Windows 32 platform x86 (using Visual Studio 2022). The compilation was successful.
  4. I created a directory Dependencies\OPCClientToolKit in same directory of .sln file, Dependencies\OPCClientToolKit contains two directories include and lib.
  5. I put in Dependencies\OPCClientToolKit\lib the .lib - the output after compilation.
  6. I put in Dependencies\OPCClientToolKit\include the .h files of the src code of sdk library.
  7. I added a dependency .h files via Sampler Project > Properies > C++ General. See pictures below.
  8. I added a dependency folder Dependencies\OPCClientToolKit\lib via Sampler Project > Properies > Linker General. See pictures below.
  9. I added a dependency OPCClientToolKit.lib via Sampler Project > Properies > Linker Input. See pictures below.

As far as I know all my step seems valid and fine.

But when, I am compiling (hitting Build via Visual Studio) the Sampler project code (see the code below) I am getting an error message:

Error Message:

1>------ Build started: Project: Sampler, Configuration: Release Win32 ------
1>Sampler.cpp
1>Sampler.obj : error LNK2001: unresolved external symbol "public: static class COPCHost * __cdecl COPCClient::makeHost(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?makeHost@COPCClient@@SAPAVCOPCHost@@ABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z)
1>C:\Users\Mark\Desktop\Projects\PID\OPC_DA\Release\Sampler.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Sampler.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please explain to me how should I resolve this issue. I just want to include and use this library and not to modify it.

Dependecies\OPCClientToolKit\lib

Dependecies\OPCClientToolKit\include

enter image description here

Sampler Project > Properties > C++ > General

enter image description here

Sampler Project > Properties > Linker > General

enter image description here

Sampler Project > Properties > Linker > Input

enter image description here

I am using this library (OPC client sdk):

https://sourceforge.net/projects/opcclient/

A usage example made by other guy on the internet:

https://github.com/Tibalt/OPC_DA/blob/master/OPCClientDemo/OPCClientDemo.cpp

My code just use functions and objects of that library.

In details, just trying to initialize opc connection to a server on localhost:

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <sys\timeb.h>
#include "opcda.h"
#include "OPCClient.h"
#include "OPCHost.h"
#include "OPCServer.h"
#include "OPCGroup.h"
#include "OPCItem.h"
using namespace std;

int main(void) {
    COPCClient::init();
    cout << "Done Init";
    cout << endl;
    CString hostName = "localhost";
    COPCHost* host = COPCClient::makeHost(hostName);
}

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

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

发布评论

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

评论(1

脱离于你 2025-01-20 19:24:03

该项目 OPCClientToolKit 非常古老。它看起来像是使用 ANSI 字符集构建的,没有使用宏 _UNICODE。您应该

  1. 从项目的预处理器设置中删除宏 _UNICODE
  2. 或者将整个项目 OPCClientToolKit 添加为 Visual Studio 解决方案的依赖项,并将宏 _UNICODE 添加到该项目。
  3. 或者使用该项目OPCClientToolKit的解决方案并使用宏_UNICODE重建静态库OPCClientToolKit.lib。

That project OPCClientToolKit is very old. It looks like built with using ANSI character set, without using the macro _UNICODE. You should either

  1. Remove the macro _UNICODE from the preprocessor settings in your project.
  2. Or add that entire project OPCClientToolKit as a dependency to your visual studio solution and add the macro _UNICODE to that project.
  3. Or use the solution of that project OPCClientToolKit and rebuild the static library OPCClientToolKit.lib with the macro _UNICODE.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文