I am a C language beginners, I encountered a problem, can not find the header file, but in fact, these header files are in the current file, I saw online methods (for example : solution) are to add - I option can solve this problem, but I am very curious, why can't it find itself, can only rely on - I option?
include path:
ls .
include test_ffmpeg.c
ls include/libavcodec/
avcodec.h avfft.h dirac.h dxva2.h vaapi.h vdpau.h videotoolbox.h xvmc.h
avdct.h d3d11va.h dv_profile.h qsv.h vda.h version.h vorbis_parser.h
source tree:
root
|-----test_ffmpeg.c
|-----include
code:
#include <stdio.h>
#include "./include/libavcode/avcodec.h"
#include "./include/libvformat/acfomat.h"
#include "./include/libavfilter/avfilter.h"
int main(void)
{
return 0;
}
compile:
gcc test_ffmpeg.c -lavcodec -lavdevice -lavfilter -lavformat -lavutil
a fatal error occured:
test_ffmpeg.c:3:10: fatal error: ./include/libavcode/avcodec.h: No such file or directory
#include "./include/libavcode/avcodec.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Your include statement mentions include/libavcode, but the path that exists is include/libavcodec.
Add a c and you should see a difference.
Related
This question already has answers here:
ffmpeg missing config.h in libavutil (No Such File or Directory) CodeBlocks
(2 answers)
Closed 4 years ago.
Translated.
I have a problem. When I try to compile a file in which h files ffmpeg are connected using gcc, I get an error that in the code ffmpeg is incorrectly written include, and it means that the file is in the current directory, although it should be in the next directory. How to fix it?
Includes in my C file:
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
Error:
D:\gcc\bin>gcc -o a a.c -lavutil -lavformat -lavcodec -lz -
lavutil -lm
In file included from a.c:3:0: libavcodec/avcodec.h:31:33:
fatal error: libavutil/samplefmt.h: No such file or directory
#include "libavutil/samplefmt.h"
^
compilation terminated.
My answer:
I was able to solve the problem only by fixing the source code. First I added the libavutil/avconfig.h file with the contents:
#ifndef AVUTIL_AVCONFIG_H
#define AVUTIL_AVCONFIG_H
#define AV_HAVE_BIGENDIAN 0
#define AV_HAVE_FAST_UNALIGNED 0
#endif
Then if I saw an error where the directory and the file (libavutil/avutil) participate, I added "../" there, since this is the case where the directory is located next to the one where the file that caused the error is (../libavutil/avutil.h). In another case, if this file should be inside (only if the directory name matches the current one (libavutil/avutil.h)), you can either just leave the file name (avutil.h), or also add "../", the difference will not be. Such operations need to be repeated about 20 times until errors in all header files disappear. After that, you can safely compile ffmpeg without special knowledge and patches. Code for avconfig.h found on the Chinese site.
your 'compile' statement is missing the location of the header file. Suggest:
the compile/link statement should be similar to:
gcc -o a a.c -I./libavutil -lavutil -lavformat -lavcodec -lz -lavutil -lm
Caveat: I wrote the -I parameter as if the current/working directory contains libavutil as a sub directory. If this is not correct, then that parameter will need to be tweaked.
On Macbook, I am making something with json-c(https://github.com/json-c/json-c)
gcc a.c
a.c:1:10: fatal error: 'json.h' file not found
#include "json.h"
^
1 error generated.`>
when I try to compile it,
it prints out error, but I have json.h file in include file.
> cd /usr/local/include/json-c/
> ls
arraylist.h json_config.h json_tokener.h
bits.h json_inttypes.h json_util.h
debug.h json_object.h linkhash.h
json.h json_object_iterator.h printbuf.h
json_c_version.h json_object_private.h random_seed.h
/usr/include/json-c/json.h is definitely exists
It's likely that the compiler doesn't have the json-c subdirectory in its include path.
With luck, you can just add that to your inclusion:
#include "json-c/json.h"
This only works if that header is stand-alone, i.e. it doesn't reference any further headers in json-c/.
If that fails you have to tell the compiler when you invoke it:
$ gcc -I/usr/local/include/json-c/ a.c
I have these 3 files under /Users/koraytugay
checksum.h
enc.h
libsec.a
libsec.a is an archive file existing of checksum.o and enc.o
Korays-MacBook-Pro:~ koraytugay$ nm libsec.a
libsec.a(enc.o):
0000000000000090 s EH_frame0
0000000000000000 T _enc
00000000000000a8 S _enc.eh
U _strlen
libsec.a(checksum.o):
0000000000000078 s EH_frame0
0000000000000000 T _checkSumFor
0000000000000090 S _checkSumFor.eh
Korays-MacBook-Pro:~ koraytugay
This is how I try to compile my hello.c file:
Korays-MacBook-Pro:HelloWorld koraytugay$ gcc hello.c -L/Users/koraytugay -libsec -o koko.out
hello.c:4:10: fatal error: 'enc.h' file not found
#include <enc.h>
^
1 error generated.
What am I doing wrong?
Btw, hello.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <enc.h>
#include <checksum.h>
// code here..
You try to include enc.h, but it's not in your include path. You can add this to your gcc invocation to fix that:
-I/Users/koraytugay
In addition to the lack of -I. switch to bring the current directory into the include path, the link specification should read -lsec, not -libsec. The linker takes the string after the -l switch, prepends lib, and looks for that. In other words, -lfoo implies that there should be a libfoo.a (static) or libfoo.so (shared) file on the link path (which itself is specified with the -L switch).
There are two ways to #include a file: with "..." and with <...>.
Essentially both are implementation-defined:
<...> searches "a sequence of implementation-defined places", which is commonly referred as the include path.
"..." searches "in an implementation-defined manner", which is usually the directory of the parent file. If the file isn't found, the <...> way (include path) is used instead.
The first way should be used for system headers as well as headers of libraries not directly included in the project, while the second way is to be preferred for headers belonging directly to the project.
Is there a compiler or preprocessor flag that will force gcc to treat #include <x.h> like it would #include "x.h"? I have a bunch of generated code that uses #include <> for files in the current directory, and gcc reports No such file or directory for these files. I'm looking for a workaround that doesn't involve editing the code.
EDIT: -I. doesn't do it. Let's say I have the following files:
foo/foo.h:
#include <foo2.h>
foo/foo2.h:
#define FOO 12345
xyz/xyz.c
#include <stdio.h>
#include "foo/foo2.h"
int main(void)
{
printf("FOO is %d\n", FOO);
return 0;
}
If, inside the xyz directory, I compile with gcc -o xyz I.. xyz.c, the compile fails:
In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)
Adding -I. doesn't change anything.
But, if I change foo/foo.h to:
#include "foo2.h"
Then the compile works. I know I could add -I../foo to my command line, but I was looking for a more generic way to treat #include <> as #include "". Does one exist?
Yes, you can pass the switch -I . to the compiler to add the current directory to the include search path.
The -I- option might help you. From gcc's man page:
-I- Split the include path. Any directories specified with -I options
before -I- are searched only for headers requested with
"#include "file""; they are not searched for "#include <file>". If
additional directories are specified with -I options after the -I-,
those directories are searched for all #include directives.
I have a problem with Nvidia's OpenCl/Cuda framework, but I think it is a gcc linking issue.
The opencl_hello_world.c example file uses following header file:
#include "../OpenCL/common/inc/CL/opencl.h"
with opencl.h using these header files:
#include <../OpenCL/common/inc/CL/cl.h>
#include <../OpenCL/common/inc/CL/cl_gl.h>
#include <../OpenCL/common/inc/CL/cl_gl_ext.h>
#include <../OpenCL/common/inc/CL/cl_ext.h>
So all the header files are in the same folder.
When I then compile with gcc opencl_hello_world.c -std=c99 -lOpenCL I get following error messages:
error: ../OpenCL/common/inc/CL/cl.h: No such file or directory
error: ../OpenCL/common/inc/CL/cl_gl.h: No such file or directory
...
Even though cl.h and the other header files are located in this folder.
Having searched SO, I then changed the includes in the opencl.h to
#include "cl.h"
#include "cl_gl.h"
how I have read here: gcc Can't Find a Included Header.
But messing around with the frameworks header files does not seem like the way to go? What would be the proper way to handle this problem?
You're using both #include "" form and #include <>, which don't search in the same paths. "" is local to your project, and the -i command line specified to gcc, <> is the 'system' path specified by -I to gcc.
You probably need to set the include path with -Ipath/to/includes in gcc's command line.