我的测试解决方案的结构如下:
TestApp (solution)
├─ TestApp (project)
├─ Submodules
├─ TestLib (solution)
├─ TestLib (project)
├─ 3rd_party
| ├─ include
| ├─ nlohmann
├─ pch.h
├─ TString.h
├─ TargetEntity.h
字符集nlohmann
是JSON库。TestLib
是一个静态库,它包含在TestApp
中作为引用。TestLib
是TestApp
repo中的git子模块。TestApp
在Additional include directories
中也有以下路径:
$(SolutionDir)Submodules\TestLib\TestLib\3rd_party\include;
$(SolutionDir)Submodules\TestLib\;
%(AdditionalIncludeDirectories)
型
下面是完整的代码:
TestApp
main.cpp
:
#include "TestLib/TargetEntity.h"
#include "TestLib/TString.h"
//#include "nlohmann/json.hpp"
int main()
{
//nlohmann::json json;
//json["abc"] = 765;
TargetEntity te;
TString ts;
}
型
TestLib
pch.h
:
#ifndef PCH_H
#define PCH_H
#define WIN32_LEAN_AND_MEAN
#include "TString.h"
//#include "nlohmann/json.hpp"
#endif // PCH_H
型pch.cpp
:
#include "pch.h"
型TString.h
:
#pragma once
#include <string>
using TString = std::wstring;
型TargetEntity.h
:
#pragma once
//#include "TString.h" // Uncomment to get rid of error
//#include "nlohmann/json.hpp" // Uncomment to get rid of error
class TargetEntity
{
public:
virtual ~TargetEntity() = default;
//void InitializeFromJson(const nlohmann::json& json) {}
protected:
TString url;
};
型TargetEntity.cpp
:
#include "pch.h"
#include "TargetEntity.h"
型TestLib
作为单独的解决方案编译时很好,当作为TestApp
内部的项目编译时(我的意思是,当你只编译lib时)。但是当我试图编译TestApp
VS时,在TargetEntity
内部显示错误:
Build started...
1>------ Build started: Project: TestLib, Configuration: Debug x64 ------
1>pch.cpp
1>TargetEntity.cpp
1>TestLib.vcxproj -> ...\Temp\TestApp\Build-TestLib-x64-Debug\TestLib.lib
2>------ Build started: Project: TestApp, Configuration: Debug x64 ------
2>TestApp.cpp
2>...\Temp\TestApp\Submodules\TestLib\TestLib\TargetEntity.h(13,13): error C3646: 'url': unknown override specifier
2>...\Temp\TestApp\Submodules\TestLib\TestLib\TargetEntity.h(13,16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>Done building project "TestApp.vcxproj" -- FAILED.
型
所以,在TestApp
的编译过程中,VS似乎忽略了TestLib
的预编译头。我知道,multi-repo
是一个预览功能,但我的情况对我来说似乎是“显而易见的”,可能有人知道如何解决这个问题。
我注意到在TargetEntity
中包含相应的头文件解决了这个问题,但我不想在整个库中这样做。
**更新:**我添加了完整的代码和完整的构建输出。为了简单起见,我还注解了nlohmann相关的代码-问题仍然存在。
1条答案
按热度按时间7ajki6be1#
pch.h在你的main.cpp和
#include pch.h
头文件之前丢失。字符集