【问题标题】:MFC C++ static library linked with non MFC console app与非 MFC 控制台应用程序链接的 MFC C++ 静态库
【发布时间】:2017-07-17 11:01:07
【问题描述】:

我正在尝试编译一个控制台程序,该程序使用实现 CString 的静态库。控制台应用程序是使用 VS 向导创建的: Win32 控制台应用程序具有预编译的标头、SDL 验证但没有 ATL 或 MFC。 静态库是一个 MFC 静态库(向导构造)。

我的错误在哪里?

这是我长期尝试的:

我使用 MFC 控件创建了一个新的控制台应用程序 - 使用静态库可以很好地编译。

然后我在必要时控制和修改了每个链接选项,比较了 2 个控制台项目。 但是第一个控制台应用程序无法编译。 我被困住了! 我在 Windows 10 上使用 Visual Studio 2012。

这里是代码: 文件 TestLib.h

#pragma once
#include <atlstr.h>

class TestLib
{
public:
    TestLib(){};
    TestLib(const CString &tst);
    virtual ~TestLib(void);
private:
    CString _tst;
};

Fichier TestLib.cpp

#include "stdafx.h"
#include "TestLib.h"

TestLib::TestLib(const CString &tst)
    : _tst(tst)
{
}

TestLib::~TestLib(void)
{
}

Fichier ConsoleTest2.cpp

// ConsoleTest2.cpp : définit le point d'entrée pour l'application console.
#include "stdafx.h"
#include "TestLib.h"

int _tmain(int argc, _TCHAR* argv[])
{
    TestLib *tst = new TestLib(); // This compile fine !
    //TestLib *tst = new TestLib(_T("Test")); // This generates LNK2019 link error
    return 0;
}

【问题讨论】:

  • 您在 stdafx.h 中包含了哪些文件?使用 MFC 时,使用 ATL 标头是不够的。您必须包含 afx.h!否则请随意仅使用 ATL(CString 也是 ATL 的一部分的模板。
  • stdafx.h文件包括targetver.h、stdio.h和tchar.h...我只是使用项目向导生成器获取标准文件,然后添加Lib类。

标签: c++ visual-studio-2012 mfc static-libraries


【解决方案1】:

这是解决方案,感谢 Jochen Arndt

只需要将 TestLib 声明更改为

TestLib::TestLib(LPCTSTR str)
    : _tst(str)
{
}

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2012-05-14
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多