Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc: undefined reference to

I would like to compile this.

program.c

#include <libavcodec/avcodec.h>  int main(){     int i = avpicture_get_size(AV_PIX_FMT_RGB24,300,300); } 

Running this

gcc -I$HOME/ffmpeg/include program.c 

gives error

/tmp/ccxMLBme.o: In function `main': program.c:(.text+0x18): undefined reference to `avpicture_get_size' collect2: ld returned 1 exit status 

However, avpicture_get_size is defined. Why is this happening?

like image 476
jamie_y Avatar asked Mar 15 '14 16:03

jamie_y


People also ask

How do I fix undefined reference error in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you fix undefined symbol in C?

To allow undefined symbols, as in the previous example, use the link-editor's -z nodefs option to suppress the default error condition. Take care when using the -z nodefs option. If an unavailable symbol reference is required during the execution of a process, a fatal runtime relocation error occurs.

How do I fix linker error in C++?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What does undefined reference to main mean in C++?

Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs.


2 Answers

However, avpicture_get_size is defined.

No, as the header (<libavcodec/avcodec.h>) just declares it.

The definition is in the library itself.

So you might like to add the linker option to link libavcodec when invoking gcc:

-lavcodec 

Please also note that libraries need to be specified on the command line after the files needing them:

gcc -I$HOME/ffmpeg/include program.c -lavcodec 

Not like this:

gcc -lavcodec -I$HOME/ffmpeg/include program.c 

Referring to Wyzard's comment, the complete command might look like this:

gcc -I$HOME/ffmpeg/include program.c -L$HOME/ffmpeg/lib -lavcodec 

For libraries not stored in the linkers standard location the option -L specifies an additional search path to lookup libraries specified using the -l option, that is libavcodec.x.y.z in this case.


For a detailed reference on GCC's linker option, please read here.

like image 104
alk Avatar answered Sep 24 '22 23:09

alk


Are you mixing C and C++? One issue that can occur is that the declarations in the .h file for a .c file need to be surrounded by:

#if defined(__cplusplus)   extern "C" {                 // Make sure we have C-declarations in C++ programs #endif 

and:

#if defined(__cplusplus)   } #endif 

Note: if unable / unwilling to modify the .h file(s) in question, you can surround their inclusion with extern "C":

extern "C" { #include <abc.h> } //extern 
like image 26
Technophile Avatar answered Sep 25 '22 23:09

Technophile