Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

c - 为什么在尝试编译C文件时出现链接器错误?(Why do I get a linker error while I try to compile a C file?)

I've had this error for weeks I already made a post about it but it wasn't very clear.

(我已经有数周的时间出现了这个错误,我已经对此发表了一篇文章,但不是很清楚。)
So I am calling a function from aa header file myBmpGris.h and the functions are implemented on the file myBmpGris.c .

(所以我从头文件myBmpGris.h调用一个函数,并且这些函数在文件myBmpGris.c上实现。)

Here is my main file:

(这是我的主文件:)

#include<stdio.h>
#include<stdlib.h>
#include "myBmpGris.h"

int main(){

    char * image_name = "image_carre.bmp";
    BmpImg image = readBmpImage(image_name);

 return 0;


I compile by using ggc main.c and I get this error message :

(我通过使用ggc main.c编译,并收到以下错误消息:)

Undefined symbols for architecture x86_64:

(架构x86_64的未定义符号:)
"_readBmpImage", referenced from:

(“ _readBmpImage”,引用自:)
_main in main-1c453a.o

(_main在main-1c453a.o中)
ld: symbol(s) not found for architecture x86_64

(ld:找不到架构x86_64的符号)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

(clang:错误:链接器命令失败,退出代码为1(使用-v查看调用))

I read a lot of posts about the same error message but none of the answers seem to apply to my case.

(我读了很多关于同一错误消息的文章,但似乎没有答案适用于我的情况。)

I'm kind of desperate because a lot of my programs give me the same error.

(我有点绝望,因为我的很多程序都给我同样的错误。)

What should I do ?

(我该怎么办 ?)

  ask by Sam_Bad translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to tell the compiler about all the code files which contain any of the needed functions.

(您需要告诉编译器所有包含任何所需功能的代码文件。)

So if you have until now compiled like gcc main.c , then the simplest way of also getting the other file compiled is gcc main.c myBmpGris.c .

(因此,如果您到目前为止像gcc main.c一样进行gcc main.c ,那么也可以对其他文件进行编译的最简单方法是gcc main.c myBmpGris.c 。)

You might want to read up on the other things you can helpfully tell the compiler (and other parts of the building), ie the possible commandline parameters.

(您可能需要阅读其他可以帮助告诉编译器(以及建筑物的其他部分)的信息,即可能的命令行参数。)

Or use one of the available free programming environments.

(或使用可用的免费编程环境之一。)

(I am not going to name any. Just use your favorite search engine on "C IDE free" or similar. The first few hits discuss several, try a few, then use the one your friends use, or the one you really like much, much better.)

((我不会透露任何名字。只需在“ C IDE free”或类似版本上使用您最喜欢的搜索引擎即可。前几首热门歌曲会讨论几首,然后尝试几首,然后使用您的朋友使用的某个,或者您真正喜欢的一个, 好多了。))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...