You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Visual Studio 2022 Community多仓库解决方案编译问题

编译TestApp时TestLib预编译头未被识别的问题

解决方案目录结构

TestApp (solution)  
├─ TestApp (project)    
├─ Submodules  
          ├─ TestLib (solution)   
                ├─ TestLib (project)  
                      ├─ 3rd_party  
                      |      ├─ include  
                      |            ├─ nlohmann  
                      ├─ pch.h  
                     ├─ TString.h  
                     ├─ TargetEntity.h

环境说明

  • nlohmann是JSON库
  • TestLib是静态库,被TestApp引用,同时是TestApp仓库的git submodule
  • TestApp的附加包含目录配置如下:
$(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" // 取消注释可解决错误
//#include "nlohmann/json.hpp" // 取消注释可解决错误

class TargetEntity
{
public:
    virtual ~TargetEntity() = default;

    //void InitializeFromJson(const nlohmann::json& json) {}

protected:
    TString url;
};

TargetEntity.cpp:

#include "pch.h"
#include "TargetEntity.h"

编译错误信息

编译TestApp时,VS报错如下:

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.

问题描述

TestLib作为独立解决方案编译,或在TestApp中单独编译均正常,但编译TestApp时出现上述错误,推测VS在编译TestApp时忽略了TestLib的预编译头。已知在TargetEntity.h中手动包含TString.h可解决错误,但不想在整个库的头文件中都添加这类手动包含。


内容的提问来源于stack exchange,提问作者qloq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:35:20