无法使用头与vscode

whitzsjs  于 2024-01-06  发布在  Vscode
关注(0)|答案(2)|浏览(273)

我试图在vscode中使用头文件,但我遇到了很多问题
这是print.c

  1. #include <stdio.h>
  2. void print1to10()
  3. {
  4. for (int i = 1; i <= 10; i++)
  5. printf("%d ", i);
  6. }

字符串
这是print.h

  1. #ifndef PRINT_TO_10_H
  2. #define PRINT_TO_10_H
  3. #include <stdio.h>
  4. void print1to10();
  5. #endif


这是主文件main.c

  1. #include <stdio.h>
  2. #include "print.h"
  3. int main()
  4. {
  5. print1to10();
  6. return 0;
  7. }


只是一个简单的程序,但似乎vscode不喜欢它,它给了我这个错误K:/test/main.c:6: undefined reference to 'print1to10' collect2.exe: error: ld returned 1 exit status

还有一件事我不知道它是否相关,但我用C/C++玩了一点。

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [
  9. "_DEBUG",
  10. "UNICODE",
  11. "_UNICODE"
  12. ],
  13. "windowsSdkVersion": "10.0.22000.0",
  14. "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
  15. "cStandard": "c99",
  16. "cppStandard": "c++17",
  17. "intelliSenseMode": "windows-gcc-x64"
  18. }
  19. ],
  20. "version": 4
  21. }


我试着把print.c&print.h文件放在同一个文件夹中的另一个文件夹中,但它给了我另一个错误,它甚至找不到头文件

  1. K:\test\main.c:2:10: fatal error: print.h: No such file or directory
  2. 2 | #include "print.h"
  3. | ^~~~~~~~~
  4. compilation terminated.

svujldwt

svujldwt1#

您遇到的问题是文件 print.c 没有链接到可执行文件。这就是为什么函数 * print 1 to 10 * 未定义。
当我运行扩展名为C/C++的main.c时,它会在文件夹 .vscode 中生成以下文件

tasks.json

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild",
  5. "label": "C/C++: gcc.exe build active file",
  6. "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
  7. "args": [
  8. "-fdiagnostics-color=always",
  9. "-g",
  10. "${file}",
  11. "-o",
  12. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  13. ],
  14. "options": {
  15. "cwd": "${fileDirname}"
  16. },
  17. "problemMatcher": [
  18. "$gcc"
  19. ],
  20. "group": {
  21. "kind": "build",
  22. "isDefault": true
  23. },
  24. "detail": "Task generated by Debugger."
  25. }
  26. ],
  27. "version": "2.0.0"
  28. }

字符串
这样你就可以在 args 部分添加文件 print.c,所以它看起来像这样:

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild",
  5. "label": "C/C++: gcc.exe build active file",
  6. "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
  7. "args": [
  8. "${file}",
  9. "${fileDirname}\\print.c",
  10. "-o",
  11. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  12. ],
  13. "options": {
  14. "cwd": "${fileDirname}"
  15. },
  16. "problemMatcher": [
  17. "$gcc"
  18. ],
  19. "group": {
  20. "kind": "build",
  21. "isDefault": true
  22. }
  23. }
  24. ],
  25. "version": "2.0.0"
  26. }


这将基本上执行以下命令:

  1. gcc main.c print.c -o main.exe


有关更多信息,请查看documentation
此外,我修改了一点你的代码,以消除冗余,但你应该正确编译。

print.h

  1. // Changed PRINT_TO_10_H to PRINT_H since the name of the
  2. // file is print.h
  3. #ifndef PRINT_H
  4. # define PRINT_H
  5. #include <stdio.h>
  6. void print1to10(void);
  7. #endif

print.c

  1. // Removed stdio.h because is already included in print.h
  2. #include "print.h"
  3. void print1to10(void)
  4. {
  5. for (int i = 0; i <= 10; i++)
  6. printf("%d ", i);
  7. }

main.c

  1. // Removed stdio.h because is already included in print.h
  2. // besides it is not used in this file.
  3. #include "print.h"
  4. int main(void)
  5. {
  6. print1to10();
  7. return 0;
  8. }

:我把所有文件都放在同一个文件夹中,如果你想把它们整理在多个文件夹中,Usman Mehwood的答案非常准确!

展开查看全部
jfgube3f

jfgube3f2#

如果您有多个文件夹的多个文件,例如具有以下结构:

  1. project
  2. |
  3. |- library_1
  4. | |
  5. | |- library_1.h
  6. | |- library_1.c
  7. |
  8. |- main.c

字符串
你可以把这样的东西传递给GCC。

  1. gcc main.c -I ./library_1 library_1/library_1.c -o program.exe


它会编译。
或者,你可以尝试我的VSCode扩展,它负责所有的构建配置,运行,调试等,还可以帮助你创建新的多库项目。
名称:C Toolkit

展开查看全部

相关问题