Why do I get a linker error while I try to compile a C file?

…衆ロ難τιáo~ 提交于 2020-01-06 05:25:13

问题


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 a a header file myBmpGris.h and the functions are implemented on the file 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 :

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

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 ?


回答1:


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.

You might want to read up on the other things you can helpfully tell the compiler (and other parts of the building), i.e. 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.)




回答2:


There are two thing.

  1. Compilation you have included. h file. It means comilper will make entry in symbol table for all used function from included library.

  2. Linking here linker try to get address from library to fill in symbol table created in first step. This cannot be performed in your case. So give full path of library.



来源:https://stackoverflow.com/questions/59107499/why-do-i-get-a-linker-error-while-i-try-to-compile-a-c-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!