简体   繁体   English

如何解决VSCODE中链接器命令失败的编译器错误这个问题

[英]how to solve this problem compiler error that linker command failed in VSCODE

I wrote some C code on VSCODE like that.我就这样在 VSCODE 上写了一些 C 代码。

The code is divided into three files: the header, the function, and main in same project folder.代码分为三个文件:header、function 和 main 在同一个项目文件夹中。 But when I started compile, files are can't compile and error.但是当我开始编译时,文件无法编译并出错。 like terminal text.像终端文本。

Maybe I think this error is linking error.. How to solve this problem..?也许我认为这个错误是链接错误..如何解决这个问题..?

[source code] [源代码]

mysqrt.c我的qrt.c

#include "mysqrt.h"
#include <stdio.h>
#include <math.h>

double mysqrt(double a, double b){
    double result = sqrt(pow(a,2)+pow(b,2));
    return result;
}

mysqrt.h文件名

#include <stdio.h>
double mysqrt(double a, double b);

mysqrtTest.c mysqrtTest.c

#include <stdio.h>
#include <math.h>
#include "mysqrt.h"

void main(void){
    double sum = mysqrt(3,4);
    printf("%.2f\n",sum);
}

[terminal text] [终端文本]

/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: warning: 
      return type of 'main' is not 'int' [-Wmain-return-type]
void main(void){
^
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: note: change
      return type to 'int'
void main(void){
^~~~
int
1 warning generated.

Undefined symbols for architecture x86_64:
 "_mysqrt", referenced from:
    _main in mysqrtTest-45c3c1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

[OS]: macOS Mojave 10.14.6 [操作系统]:macOS Mojave 10.14.6

to resolve your linking issue you need to compile all your c file, as dependencies are not automatically resolved ( header files could be name separately from code file so .h files and .c files are independant ).要解决您的链接问题,您需要编译所有 c 文件,因为依赖项不会自动解决(头文件可以与代码文件分开命名,因此.h文件和.c文件是独立的)。

# assuming that gcc is your compiler
gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c -lm

Although I would recommend you to learn about separate compilation, and using a build system, like make尽管我建议您了解单独的编译,并使用构建系统,例如 make

Example of Makefile Makefile示例

mysqrtTest: mysqrtTest.o mysqrt.o
    ${CC} -o $@ $^ -lm

then use make to build your binary然后使用make来构建你的二进制文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM