Ubuntu下VS Code编译多文件C++项目报错:未定义引用
Ubuntu下VS Code编译C++项目链接错误排查
问题现象
- 点击VS Code运行按钮编译时出现未定义引用的链接错误,提示找不到
Movies类的构造、析构及成员函数实现 - 手动执行命令
g++ Movie.cpp Movies.cpp challenge.cpp -o challenge可正常编译运行
错误信息
cd "/home/luca/develop/Cplusplus_course/Section13/" && g++ challenge.cpp -o challenge && "/home/luca/develop/Cplusplus_course/Section13/"challenge /usr/bin/ld: /tmp/ccoxepCS.o: in function `main': challenge.cpp:(.text+0x27): undefined reference to `Movies::Movies()' /usr/bin/ld: challenge.cpp:(.text+0x33): undefined reference to `Movies::display() const' /usr/bin/ld: challenge.cpp:(.text+0x9e): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)' /usr/bin/ld: challenge.cpp:(.text+0x13f): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)' /usr/bin/ld: challenge.cpp:(.text+0x1e0): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)' /usr/bin/ld: challenge.cpp:(.text+0x222): undefined reference to `Movies::display() const' /usr/bin/ld: challenge.cpp:(.text+0x28d): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)' /usr/bin/ld: challenge.cpp:(.text+0x32e): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)' /usr/bin/ld: challenge.cpp:(.text+0x370): undefined reference to `Movies::display() const' /usr/bin/ld: challenge.cpp:(.text+0x3ac): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: challenge.cpp:(.text+0x403): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: challenge.cpp:(.text+0x45a): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: challenge.cpp:(.text+0x481): undefined reference to `Movies::display() const' /usr/bin/ld: challenge.cpp:(.text+0x4bd): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' /usr/bin/ld: challenge.cpp:(.text+0x4e9): undefined reference to `Movies::~Movies()' /usr/bin/ld: challenge.cpp:(.text+0x771): undefined reference to `Movies::~Movies()' collect2: error: ld returned 1 exit status
当前tasks.json配置
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "-Wall", "${workspaceFolder}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}", ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "compiler: /usr/bin/g++" } ] }
问题原因
- VS Code的运行按钮默认绑定的是「编译并运行单个活动文件」的快捷操作,而非你配置的默认build任务
- 从错误信息里的编译命令
g++ challenge.cpp -o challenge可以看出,运行按钮只编译了当前打开的challenge.cpp,没有包含Movie.cpp和Movies.cpp,导致链接时找不到类的实现代码
解决方法
方法1:使用构建命令替代运行按钮
- 按下
Ctrl+Shift+B,选择你配置的C/C++: g++ build active file任务执行构建,构建完成后再手动运行生成的可执行文件 - 或者右键点击终端面板,选择「运行任务」执行你的build配置
方法2:修改运行按钮的绑定逻辑
- 打开「运行和调试」面板(
Ctrl+Shift+D),点击「创建launch.json文件」,选择「C++ (GDB/LLDB)」 - 在生成的
launch.json中,确保preLaunchTask字段的值和tasks.json里的label完全一致,示例如下:
{ "version": "0.2.0", "configurations": [ { "name": "C/C++: g++ 生成和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file", "miDebuggerPath": "/usr/bin/gdb" } ] }
- 保存后,点击运行按钮会先执行你配置的build任务(编译所有cpp文件),再启动调试运行
方法3:验证工作区路径
- 确认VS Code打开的工作区是
/home/luca/develop/Cplusplus_course/Section13/,否则${workspaceFolder}/*.cpp无法匹配到所有cpp文件 - 可以通过菜单栏「文件」→「打开文件夹」重新选择正确的工作目录
内容的提问来源于stack exchange,提问作者Luca90
相关产品推荐
相关产品推荐

