使用cstring编译静态库 [英] Compile static library with cstring

查看:135
本文介绍了使用cstring编译静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在尝试编译一个使用实现CString的静态库的控制台程序。



我在Windows 10上使用Visual Studio 2012.



这是代码:



File TestLib.h

Hi everybody.

I'm trying to compile a console program that uses a static library implementing CString.

I'm working with Visual Studio 2012 on Windows 10.

Here is the code :

File TestLib.h

#pragma once
#include <atlstr.h>

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





File TestLib.cpp



File TestLib.cpp

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


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


TestLib::~TestLib(void)
{
}





文件





File

// 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;
}





控制台应用程序是使用VS向导创建的:



Win32控制台应用程序,带有预编译头,SDL验证但没有ATL或MFC。

静态库是一个MFC静态库(向导构造)。



我的错误在哪里?



我的尝试:


我使用MFC控件创建了一个新的控制台应用程序 - 这可以通过静态库进行编译。



然后我已经控制并修改了必要的每个链接选项,比较了2个控制台项目。

但是第一个控制台应用程序无法编译。

我被困了!



The console app has been created with the VS wizard with :

Win32 console application with precompiled headers, SDL verification but without ATL or MFC.
The static library is a MFC static library (wizard construction).

Where is (are) my mistake(s) ?

What I have tried:

I've crated a new console app using MFC controls - this compile fine with the static library.

Then I've controlled and modified when necessary every link options, comparing the 2 console projects.
But the 1st console application does not compile.
I'm stucked !

推荐答案

很高兴看到完整的错误信息,看看哪些功能缺失。



如果您在没有MFC支持的情况下构建应用程序,则它不会与包含 CString 支持功能。因为您包含 atlstr.h ,所以您没有收到编译错误。但是在行中

It would be good to see the full error message to see which function(s) are missing.

If you build your app without MFC support, it is not linked with the library that contains the CString support functions. Because you are including atlstr.h, you are not getting compilation errors. But in the line
TestLib *tst = new TestLib(_T("Test"));

你正在创建一个临时的 CString 并且构造函数从MFC库调用函数,这导致链接器错误。



最简单的解决方案是更改第二个构造函数你的图书馆要

you are creating a temporary CString and the constructor calls functions from the MFC library which results in the linker error.

The simplest solution would be changing the second constructor of your library to

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

然后你的控制台应用程序传递一个 TCHAR * 指针而不是创建一个temporaray CString ,并且你的任务在你的内部完成与MFC库链接的库。

Then your console application passes a TCHAR* pointer instead of creating a temporaray CString and the assignment is done within your library which is linked with the MFC library.


这篇关于使用cstring编译静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆