Windows Command line c file compile error - c

I work with C codes using gcc compiler and Clion IDE
So, I have a src folder for .c codes and include folder of .h files which contains a bunch of #define
I include some of the .h files in my main.c programe
when I run it in my Clion project, everything work fine
but when I try to run with command line (cuz I need command arguments), it says
Fatal Error: MyFunction.h, no such file or directory MyFunction.h IS MY FILE, NOT DEFAULT library file
What I typed in the command line is $gcc C:\Users\Admin\CLionProjects\project\src\main.c
What I get is "C:\Users\Admin\CLionProjects\project\src\main.c 10: Fatal Error: MyFunction.h No such file or directory.
Then I try to change the #include <MyFunction.h> to #include "MyFunction.h", hoping it will solve the problem, but no.
Then I did a test, I call a function of another .c file in the same src folder, and use command line to run main.c, but it tells me the function is not even defined.
cmd can recognized all the default library files like stdio.h, but none of those created by me. Any idea how to solve this problem? I know it must be some kind of path error

You have to tell gcc preprocessor where to find files you want to include.
You have two kinds of header:
System headers: included with #include <header>.
Local headers: included with #include "header".
Preprocessor searchs in header search path to find system headers.
Preprocessor searchs in current directory, then in header path to find local headers.
You can add folders in header search path using -I option in gcc invocation.
So in your case, you can compile with these commands:
$gcc -I C:\Users\Admin\CLionProjects\project\include C:\Users\Admin\CLionProjects\project\src\main.c
or
$cd C:\Users\Admin\CLionProjects\project\src
$gcc -I ..\include main.c

Related

C and MinGW: How do I fix my "No such file or directory" error?

I have made a python "compiler" that helps me compile my C code with gcc, for example it fetches all my header files and source files. So my cmd commmand is gcc {headers} {source} -o {build_dir}/build.exe -lgdi32 -w where {headers} is a string like -Ipath/to/headers/foo.h -Ipath/to/other/headers.foo2.h and where {source} is the same but with .c files. It seems that the compiler finds the header files, but when compiling my code it fails.
(btw I am trying to make a portable programming environment on my flash drive so python and mingw are both portable)
This is the error: fatal error: test.h: No such file or directory #include "test.h"
My project tree
I have put the third party library files into the mingw directory instead of making a custom one and then linking it in the gcc command.
The -I option takes the path to the directory containing the header files or more specifically with an argument -Ipath and a directive #include<a/b.h>, the compiler will try to look for the header file at path/a/b.h.
So you should not give it paths to header files, only to the directory or directories relative to which you use include directives.

Compiling C file which includes other header files

I am trying to compile a file Mv.c like - g++ microtime.c Mv.c
It gives an error - v.c:2:10: fatal error: microtime.h: No such file or directory 2 | #include <microtime.h>
My current directory has both microtime.h and microtime.c and Mv.c includes microtime.h
I am not sure how to go about compiling it.
Since my main program Mv.c is using microtime.h do I need to compile microtime.c first and pass it as an argument to g++?
I got it compiled by using the command g++ -I. Mv.c microtime.o
where microtime.o I generated using g++ -c microtime.c
I am not sure why this command works and why we need to specify the extra -I. option when I have already created the compiled object file microtime.o
If you write #include <microtime.h>, the compiler uses its list of system directories to look for the header file. By the option -I you add a directory to this list, and the compiler is happy.
To include project-local header files, use #include "microtime.h".
Read the chapter "Directory Options" in GCC's documentation to learn more.

gcc include header and get output after preprocessing

I want the preprocessed output of a .c file, but I also want to include a header file without the macro "include..." in the .c file. Usually, you add the -I option for including a directory where headers are.
But if I want to combine -I and -E, gcc does't seem to include my header files in the specified directory.
My command:
gcc -E -I/externDefines myFirmware.c > myFirmware.preprocessed
Does anyone know what the problem could be?
-I does not mean “Include the header files from the given directory in the compilation.” It means “When searching for a file requested with #include, look for the file in the given directory.”
GCC has a command-line switch, -include file that will include a file in the compilation. However, it includes a single file, so you must list each file you want included; it will not automatically include all header files in a single directory. The command-line shell you are using may have features that help generate a list of -include switches with the file names.
A portable way to include a header file X.h while compiling Y.c without changing Y.c would be to create an auxiliary file containing:
#include "X.h"
#include "Y.c"
and then compile that instead of Y.c.

#include "existing file" fails: no such file (C)

Compiling C with gcc.
While
#include "/absolute/path/to/my/file"
works OK,
#include "../../relative/path/to/my/file"
fails with "no such file or directory". This only happens when the file is placed outside the project directory. file has read permissions. What could be the reason?
When using the format
#include "some_file.h"
the preprocessor by default looks in the same directory as the source file, if the file is not found there, it looks in the header-file search paths.
If the header file is not in the same directory as the source file, and not in one of the directories of the preprocessors search-path, then it will not be found.
You can write relative or full paths though:
#include "../some_directory/some_file.h"
Yes, you need GCC Options for Directory Search
When using gcc and local header files you need to add an include path to your build command.
mysource.c:
#include "localfile.h"
build command:
gcc -o program mysource.c
This works as long as the header file is in the same directory as your source (where you're running the command). If your header file is in a different directory you can include with the -I option:
gcc -I../headerdir -o hello.exe hello.c
or an absoulte path:
gcc -I/home/user/myprogra/headerdir -o hello.exe hello.c

Header files linked to from header file not found.

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.

Resources