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

Visual Studio 2019无法引用外部.cpp文件的问题求助

外部.cpp文件链接报错(LNK2019)的解决方法

问题背景

已将外部目录添加到项目的「附加包含目录」,编译环节正常,但调用外部.cpp中的函数时出现unresolved external symbol(未解析外部符号)错误。相关文件内容如下:

test.h(外部文件)

// test.h
// external file
#pragma once
#ifndef TEST_H
#define TEST_H

int what();

#endif

test.cpp(外部文件)

// test.cpp
// external file
#include "test.h"

int what() {
    return 1;
}

main.cpp(项目内文件)

// main.cpp
// project file
#include "test.h"
int main() {
    what(); // LNK2019 unresolved external symbol
    return 0;
}

解决方案(无需转.lib/.dll)

方法1:直接将外部.cpp文件添加到项目

右键项目 -> 「添加」-> 「现有项」,浏览到外部目录选择test.cpp完成添加。

  • 此操作仅在项目中建立文件引用,不会复制文件到项目目录,外部文件的修改会直接同步到编译流程。
  • 编译时该cpp会被正常编译,链接阶段就能找到函数的实现符号,解决报错。

方法2:通过项目属性指定附加源文件

右键项目 -> 「属性」-> 「配置属性」-> 「C/C++」-> 「所有选项」,找到「附加源文件」选项,填入test.cpp的完整路径(多文件用分号分隔)。

  • 这种方式相当于告诉编译器额外编译指定的源文件,效果和直接添加文件一致,但不如方法1直观,维护成本更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:44:53