包含自己的头文件时无法构建
Cannot build while I include own header file
我想在 Linux - VSCode 上使用 C。因此,我参照教程Using C++ on Linux in VS Code使用C/C++扩展,完成构建hello world。接下来,我想通过添加 adder.h/.c:
来测试“include .h”
//adder.h
#include <stdio.h>
int add(int a, int b);
//adder.c
#include "adder.h"
int add(int a, int b)
{
return a + b;
}
//main.c
#include <stdio.h>
#include "adder.h"
int main()
{
printf("ret = %d\n", add(1,2));
}
这些代码可以在 DevC++ 上构建,无需任何其他设置。但是,它在 Terminal -> 运行 Build Task... 后显示错误消息
:
> Executing task: C/C++: gcc build active file <
Starting build...
/usr/bin/gcc -g /home/hughesyang/Test/c/projects/multi_files/main.c -o /home/hughesyang/Test/c/projects/multi_files/main
/tmp/ccvaDYKq.o: In function `main':
/home/hughesyang/Test/c/projects/multi_files/main.c:6: undefined reference to `add'
collect2: error: ld returned 1 exit status
Build finished with error(s).
The terminal process failed to launch (exit code: -1).
我不确定是否我需要修改 DEFAULT tasks.json 来解决这个问题? 还是其他错误造成的?
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc"
}
]
}
PS。 Using C++ on Linux in VS Code 是一个 C++ 示例。因此,我将 g++ 替换为 gcc 作为 C 的编译器。
感谢@WhozCraig 的提示。 它需要使用 cmake 来完成。 在参考 Get started with CMake Tools on Linux 和 在 add_executable()[=16] 中添加 adder.c 之后=] 的 CMakeLists.txt,有效!
cmake_minimum_required(VERSION 3.0.0)
project(helloworld_cmake VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(helloworld_cmake main.c adder.c)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
我想在 Linux - VSCode 上使用 C。因此,我参照教程Using C++ on Linux in VS Code使用C/C++扩展,完成构建hello world。接下来,我想通过添加 adder.h/.c:
来测试“include .h”//adder.h
#include <stdio.h>
int add(int a, int b);
//adder.c
#include "adder.h"
int add(int a, int b)
{
return a + b;
}
//main.c
#include <stdio.h>
#include "adder.h"
int main()
{
printf("ret = %d\n", add(1,2));
}
这些代码可以在 DevC++ 上构建,无需任何其他设置。但是,它在 Terminal -> 运行 Build Task... 后显示错误消息 :
> Executing task: C/C++: gcc build active file <
Starting build...
/usr/bin/gcc -g /home/hughesyang/Test/c/projects/multi_files/main.c -o /home/hughesyang/Test/c/projects/multi_files/main
/tmp/ccvaDYKq.o: In function `main':
/home/hughesyang/Test/c/projects/multi_files/main.c:6: undefined reference to `add'
collect2: error: ld returned 1 exit status
Build finished with error(s).
The terminal process failed to launch (exit code: -1).
我不确定是否我需要修改 DEFAULT tasks.json 来解决这个问题? 还是其他错误造成的?
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc"
}
]
}
PS。 Using C++ on Linux in VS Code 是一个 C++ 示例。因此,我将 g++ 替换为 gcc 作为 C 的编译器。
感谢@WhozCraig 的提示。 它需要使用 cmake 来完成。 在参考 Get started with CMake Tools on Linux 和 在 add_executable()[=16] 中添加 adder.c 之后=] 的 CMakeLists.txt,有效!
cmake_minimum_required(VERSION 3.0.0)
project(helloworld_cmake VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(helloworld_cmake main.c adder.c)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)