VScode跨文件编译(报错:undefined reference to `a_val‘collect2.exe: error: ld returned 1 exit status)

本文介绍如何在Visual Studio Code中编写C代码时解决`undefined reference to a_val`错误,涉及头文件包含、链接配置和设置优化。详细步骤包括修改设置文件,针对.c文件扩展执行器,并提供必要的头文件和源文件示例。

问题描述

使用vscode编写C代码,导入自定义.c文件中的函数时,报错:
undefined reference to `a_val‘collect2.exe: error: ld returned 1 exit status

解决方法

项目目录

在这里插入图片描述

文件内容

头文件

// Test1.h
# pragma once

void fun1();
// Test2.h
# pragma once

void fun2();

源文件

// test1.c
#include<stdio.h>
#include<stdlib.h>
#include "../header/Test1.h"

void fun1()
{
    printf("%d\n",2);
}
// test2.c
#include<stdio.h>
#include<stdlib.h>
#include "../header/Test2.h"

void fun2()
{
    printf("%d\n",2);
}
// main.c
#include<stdio.h>
#include<stdlib.h>
#include "../header/Test1.h"
#include "../header/Test2.h"


int main()
{
    fun1();
    fun2();
    return 0;
}

修改配置

1)文件->首选项->设置

在这里插入图片描述

2)输入code runner,找到Executor Map,打开json配置文件在这里插入图片描述

3)找到c将其中的$fileName改为*.c

在这里插入图片描述在这里插入图片描述
PS:如果有需要,c++对应的cpp也可以一起更改

关注一下

如果喜欢的话,关注我吧,公众号:编码魔坊

* 正在执行任务: g++ -o C:\cpp_project/uninstall C:\cpp_project\uninstall.cpp -std=c++17 -Wall -Wextra -g C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccEOuuki.o: in function `ArduinoUninstaller::ArduinoUninstaller()': C:/cpp_project/uninstall.cpp:27:(.text$_ZN18ArduinoUninstallerC1Ev[_ZN18ArduinoUninstallerC1Ev]+0xd0): undefined reference to `__imp_CoTaskMemFree' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/cpp_project/uninstall.cpp:31:(.text$_ZN18ArduinoUninstallerC1Ev[_ZN18ArduinoUninstallerC1Ev]+0x104): undefined reference to `__imp__dupenv_s' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccEOuuki.o: in function `ArduinoUninstaller::deleteShortcuts()': C:/cpp_project/uninstall.cpp:254:(.text$_ZN18ArduinoUninstaller15deleteShortcutsEv[_ZN18ArduinoUninstaller15deleteShortcutsEv]+0xfe): undefined reference to `__imp_CoTaskMemFree' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/cpp_project/uninstall.cpp:265:(.text$_ZN18ArduinoUninstaller15deleteShortcutsEv[_ZN18ArduinoUninstaller15deleteShortcutsEv]+0x1e0): undefined reference to `__imp_CoTaskMemFree' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccEOuuki.o:uninstall.cpp:(.rdata$.refptr.FOLDERID_Desktop[.refptr.FOLDERID_Desktop]+0x0): undefined reference to `FOLDERID_Desktop' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccEOuuki.o:uninstall.cpp:(.rdata$.refptr.FOLDERID_Programs[.refptr.FOLDERID_Programs]+0x0): undefined reference to `FOLDERID_Programs' C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccEOuuki.o:uninstall.cpp:(.rdata$.refptr.FOLDERID_LocalAppData[.refptr.FOLDERID_LocalAppData]+0x0): undefined reference to `FOLDERID_LocalAppData' collect2.exe: error: ld returned 1 exit status * 终端进程&ldquo;C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command g++ -o C:\cpp_project/uninstall C:\cpp_project\uninstall.cpp -std=c++17 -Wall -Wextra -g”已终止,退出代码: 1。 * 终端将被任务重用,按任意键关闭。
最新发布
12-20
Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_9d681/fast /usr/bin/gmake -f CMakeFiles/cmTC_9d681.dir/build.make CMakeFiles/cmTC_9d681.dir/build gmake[1]: Entering directory '/home/zms/lammps/build/CMakeFiles/CMakeScratch/TryCompile-A6ur3p' Building CXX object CMakeFiles/cmTC_9d681.dir/testCXXCompiler.cxx.o /usr/bin/c++ -std=gnu++17 -o CMakeFiles/cmTC_9d681.dir/testCXXCompiler.cxx.o -c /home/zms/lammps/build/CMakeFiles/CMakeScratch/TryCompile-A6ur3p/testCXXCompiler.cxx Linking CXX executable cmTC_9d681 /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_9d681.dir/link.txt --verbose=1 /usr/bin/c++ CMakeFiles/cmTC_9d681.dir/testCXXCompiler.cxx.o -o cmTC_9d681 /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_symbind_alt@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_catch_exception@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `__nptl_change_stack_perm@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_find_dso_for_object@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `__tunable_is_initialized@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `__tunable_get_val@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_signal_exception@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_signal_error@GLIBC_PRIVATE' /home/zms/.conda/envs/mace_env/bin/ld: /lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_preinit@GLIBC_PRIVATE' collect2: error: ld returned 1 exit status gmake[1]: *** [CMakeFiles/cmTC_9d681.dir/build.make:99: cmTC_9d681] Error 1 gmake[1]: Leaving directory '/home/zms/lammps/build/CMakeFiles/CMakeScratch/TryCompile-A6ur3p' gmake: *** [Makefile:127: cmTC_9d681/fast] Error 2
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道人禅(armey)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值