undefined reference to function declared in *.h file

后端 未结 4 786
挽巷
挽巷 2020-12-10 16:41

I am a unskilled programmer and new to linux, I run into a problem when complining. I have two files \'ex_addinst.c\' and \'lindo.h\' in the same folder, I input command :

相关标签:
4条回答
  • 2020-12-10 17:21

    undefined reference to ... is not a declaration problem. The compiler fails because it can't find symbols (objects) which are related to those declared functions.
    In your case, you use the Limbo API, and include the header file, but you don't tell the compiler to link with the library : that's why it doesn't find symbols.
    EDIT : I had forgotten the part when you say you're new to Linux. To link with the library, you need to use the -L/-l options of g++. man g++ is always a good read, and the Limbo's documentation should be, too.

    0 讨论(0)
  • 2020-12-10 17:22

    You need to tell gcc to link with the library or object file(s) that contain the LS... functions you're using. The header file tells the compiler how to call them, but the linker needs to know where to get the compiled code from.

    0 讨论(0)
  • 2020-12-10 17:25

    Ok, lindo.h contains the prototypes for those functions, but where are the functions actually defined? If they're in another C file you need to compile that one too, and link both the object files together.

    If the functions are part of another static library, you need to tell the linker to link that library along with your object file.

    If they're defined with a shared library, you can probably get g++ to still link to it at compile time, and take care of the library loading etc. Otherwise you'll need to load the library at runtime and reference the functions from the library. This Wikipedia article on dynamic loading of shared libraries contains some example code.

    0 讨论(0)
  • 2020-12-10 17:35

    Try

    g++ -Wall -o ex_addinst ex_addinst.c

    instead of

    g++ -Wall -o ex_addinst ex_addinst.o

    You want to compile the .c file, not the .o file.

    0 讨论(0)
提交回复
热议问题