# First test **I want to succeed this way**
SCREENSHOT BELOW
In folder Source file controller.c
In folder Headers file controler.h
In folder .vscode file c_cpp_properties.json
In folder .vscode file launch.json
#include <stdlib.h>
#include <stdio.h>
#include "controller.h"
int main(){
double resultat;
resultat = airRectangle(10.0,20.0);
printf("L'aire du rectangle est %f",resultat);
return 0;
}
`Give me
fatal error No such file or directory`
`#include "controller.h"
| ^~~~~~~~~~~~~~`
`# Second test
It works correctly this way for small projects so please help me
`
I watch on stack overflow and youtube for all answer.
2
Answers
You could use CMake and Ninja to handle these (relatively?) complex folder structures.
Instead of remembering what complicated commands you have to give to the compiler and changing it every time you add a new file.
As I see, this is your folder structure
For this, you can have a CMake file like this.
This file should be saved as
CMakeLists.txt
in the "application" folder.Then in terminal, use Cmake and Ninja to build.
This will build and place your program executable in the
build
folder.Ps, you should consider making your project structure like this:
Quoted from GCC documents:
When the compiler compiles
main.c
during your first test, the directory of the current file issource
. But the preprocessor couldn’t findcontroller.h
undersource
directory, therefore it throws a "No such file or directory" error. It won’t find you the file in theHeaders
directory.This is your file structure you showed: (Edited from @Usman Mehmood’s answer)
In your second test, you moved/copied
controller.h
to thesource
directory, and changed the include file tocontroller.c
. This time the preprocessor foundcontroller.c
and replaced#include "controller.c"
to the code in the file. Then it replaced#include "controller.h"
with the code incontroller.h
. Therefore the code compiles correctly.Technically speaking, the following code in your
controller.h
only includes the declaration of theairRectangle
function, but not the definition of the function.The definition is included in
controller.c
:The compiler would throw a "undefined reference" error if you simply move/copy
controller.h
to theHeader
directory. I suggest you to move the definition of the function tocontroller.h
.To solve this problem, you can:
move/copy
controller.h
tosource
directory.tell the compiler to look for
controller.h
in theHeader
directory by using the-I
compile parameter. Quoted from this article:For example, you can use
-Ifoldername
to tell the compiler to find for header file in thefoldername
directory. To let the compiler findcontroller.h
, you can add-IHeaders
to the compile arguments(-I C:/Users/fredd/OneDrive/Bureau/Application/Headers
for full path).