Including a header file from another directory - c

I have a main directory A with two sub directories B and C.
Directory B contains a header file structures.c:
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
Directory C contains main.c code:
#include<stdio.h>
#include<stdlib.h>
#include <structures.h>
int main()
{
stud *value;
value = malloc(sizeof(stud));
free (value);
printf("working \n");
return 0;
}
But I get an error:
main.c:3:24: error: structures.h: No such file or directory
main.c: In function ‘main’:
main.c:6: error: ‘stud’ undeclared (first use in this function)
main.c:6: error: (Each undeclared identifier is reported only once
main.c:6: error: for each function it appears in.)
main.c:6: error: ‘value’ undeclared (first use in this function)
What is the correct way to include the structures.h file into main.c?

When referencing to header files relative to your c file you should use #include "path/to/header.h"
The form #include <someheader.h> is only used for internal headers or for explicitly added directories (in gcc with the -I option).

write
#include "../b/structure.h"
in place of
#include <structures.h>
then go in directory in c & compile your main.c with
gcc main.c

If you work on a Makefile project or simply run your code from command line, use
gcc -IC main.c
where -I option adds your C directory to the list of directories to be searched for header files, so you'll be able to use #include "structures.h"anywhere in your project.

If you want to use the command line argument then you can give gcc -idirafter ../b/ main.c
then you don't have to do any thing inside your program.

Related

Undeclared identifier with definition in header file

include "spinach.h" is included in the main.c file
main.c makes a function call to a struct defined in spinach.h header. The function call occurs in the main.c function, and is the first reference to this type Lexer. The main.c file runs without error absent this line.
Lexer *lex = malloc(sizeof(Lexer));
The definition is on the 0 indentation level (it is not defined in local scope); it is given below in triple quotes. In the header (spinach.h)
typedef struct {
char **lines;
char* current;
int pos;
int line;
int col;
int* indentlevel;
int indentcap;
int indentlen;
} Lexer;
What happpens?
 bash run.sh
main.c:115:3: error: use of undeclared identifier 'Lexer'
Lexer *lex;
^
main.c:115:10: error: use of undeclared identifier 'lex'
Lexer *lex;
^
2 errors generated.
Any variation that includes Lexer, including using pointers, references to malloc or the output of a custom initializer function will throw related errors.
Looked for multiple references (same global variable in 2 files in main.py references) to Lexer. All references on the internet are examples wherein the header file is not included, the variable or function is defined after the reference or call, or is absent entirely, else the error will show where there shows a reference in a global scope to a local definition. I do not see this to be the case. All references to custom struct lever in the main.c file, main function will throw this error. If the definitions are placed in the main.c file itself, instead of the header spinach.h referenced, it runs without error.

Multiple definitions of 'some variable' which is declared in a header file

I have a header file command.h which contains all my variables and function declarations
//command.h
int someVar1;
int someVar2;
void modifying_loop (int a, int b);
int someVar3;
.
.
.
In another file my_algorithm.c I define the previously declared function modifying_loop and use some of the variables declared in the header in
//my_algorithm.c
#include "command.h"
void modifying_loop (int x, int y)
{
someVar1 = x+2;
someVar2 = y+2;
}
And I have my main file command.c I called the modifying_loop function like this :
#include "command.h"
int main ()
{
modifying_loop(5,6);
return 0;
}
I compile the it using gcc -o command command.c -lm -lpigpio -L/usr/lib/ which returns me
undefined reference to modifying_loop'
Then to tackle that I link the my_algorithm.c file by using
gcc -o command command.c my_algorithm.c -lm -lpigpio -L/usr/lib/ which gives me the following :
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here
and same errors for the rest of the variables declared in the header file. Does anyone have any idea what is causing the errors.
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here
you have to make sure, the same code won't be included multiple times.
You can do this by embedding the header file code in #ifndef #define #endif macros. These are called include guards
The rest of your code seems to be working fine.

Enumerations in C head files shared across multiple files

I want to define an enumeration type ONCE and have that type be shared across all the other files when I include the file, however I keep getting the following errors:
$ gcc -std=c99 main.c invoc.h invoc.c
main.c: In function ‘main’:
main.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pr_alg’
main.c:12: error: ‘pr_alg’ undeclared (first use in this function)
main.c:12: error: (Each undeclared identifier is reported only once
main.c:12: error: for each function it appears in.)
main.c:13: error: ‘FIFO’ undeclared (first use in this function)
invoc.c:7: error: expected ‘)’ before ‘myalg’
The code is as follows:
invoc.h:
#define INVOC_H
#ifndef INVOC_H
typedef enum {FIFO, SECOND_CHANCE, RANDOM, NRU, CLOCK, AGING} alg_t;
void func1(alg_t myalg);
#endif
invoc.c:
#include "invoc.h"
void func1(alg_t myalg) {
myalg = NRU;
}
main.c:
#include "invoc.h"
int main(int argc, char **argv) {
extern alg_t pr_alg;
pr_alg = FIFO;
printf("PR_ALG: %d\n", pr_alg);
return 0;
}
Is there any way that I can define an enumeration in a .h file, and include it in all other files so that I can both create different variables of that type and pass it to functions?
You have an error in your invoc.h file:
#define INVOC_H
#ifndef INVOC_H
...
#endif
You first define a macro INVOC_H, then check if it does not exists (it does), so the code inside is removed by the preprocessor and not parsed by the compiler.
It should be:
#ifndef INVOC_H
#define INVOC_H
...
#endif
After this change your code will work fine.
You don't compile .h files, only .c files. That's why we put all definitions in .c files, and only declarations in .h files. To compile, just do:
gcc -std=c99 mmu.c invoc.c
You declare pr_alg in main() as extern variable. If the line you provided is the whole compilation line, the compile will issue linker error as variable pr_alg is nowhere defined. Remove extern or define variable pr_alg with global storage duration in one of the .c files.

Not able to handle multiple c files and header files during compilation

I read that the C file containing the function definition should be of the same name as the header file. So, i created two files: functions.h, functions.c & lastly the main.c file which calls the functions which are defined inside of the functions.c file.
//functions.h file
void check();
I have declared check function in the header file
//functions.c file
#include <stdio.h>
#include "functions.h"
int main(void){
void check(){
printf("\nThis is a Test\n");
}
return 0;
}
This file contains all the function definition. But one thing i want to clear out is, I saw some another question on stackoverflow of basically the same type but in function file he had just included the header files and function definitions, without main(). Shouldn't that .c file throw an error?
//main.c file
#include "function.h"
#include <stdio.h>
int main(void)
{
check();
return 0;
}
when i open terminal and type the command to compile the code:
clang main.c
I get an error:
Undefined symbols for architecture x86_64:
"_check", referenced from:
_main in heap-22db64.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
May be i haven't compiled functions.c file that's the reason i see this. I am just compiling main.c. I am not sure about this linking error. If i have 35 .c files. Compiling all of them via commandline would be harder task? What should be my approch to deal with these big projects. Having multiple C & header files?
Here's the typical scenario:
// functions.c
void check(void) {
// do stuff
}
Note: just the definition of check, and nothing else. Then a header:
// functions.h
extern void check(void);
Just a declaration. Then the main file:
// main.c
#include "functions.h"
int main(int argc, char *argv[]) {
check();
}
When definitions are provided in another file, you have to specify that like so using the extern keyword:
functions.h:
extern void check();
functions.c:
void check()
{
printf("\nThis is a Test\n");
}

C Include .h not working?

I am new to C programming. When I include the blank.h file into the Test.c file the program will not compile, however when I include blank.c file into the Test.c file it compiles fine. Below is the source for all the .c and .h files. Im using gcc as my compiler, and I have a feeling I need to do some sort of linking with it? Any help would be great thanks!
This is the Test.c source
#include <stdio.h>
#include "blank.h"
#include "boolean.h"
int main()
{
bool result = blank("");
printf("%d\n", result);
return 0;
}
This is the blank.h source
// Header file for blank function
bool blank(char string[]);
This is the blank.c source
#include "boolean.h"
#include "blank.h"
#include <regex.h>
bool blank(char string[])
{
regex_t regex_blank;
int blank = regcomp(&regex_blank, "[:blank:]", 0);
blank = regexec(&regex_blank, string, 0, NULL, 0);
if ( string == NULL || blank == 1 )
return true;
else
return false;
}
and finally the boolean.h
// Boolean
// Define true
#ifndef true
#define true 1
#endif
// Define false
#ifndef false
#define false 0
#endif
typedef int bool;
Ok, so I tried the source code you provided. There were a couple problems. Here are the exact steps of how I built, what I fixed. See if this works for you:
Created 4 files in a folder: Test.c, blank.c, blank.h and boolean.h
Copied code over.
From the shell ran:
gcc Test.c blank.c -o b
Output:
In file included from Test.c:2:0:
blank.h:3:1: error: unknown type name ‘bool’
blank.c: In function ‘blank’:
blank.c:11:46: error: ‘NULL’ undeclared (first use in this function)
blank.c:11:46: note: each undeclared identifier is reported only once for each function it appears in
To fix the first error:
In blank.h added this on top: #include "boolean.h"
To fix the second error:
In blank.c added this after the other includes: #include <stdlib.h>
Once again the terminal ran:
gcc Test.c blank.c -o b
then from the terminal ran ./b and it prints 1.
I suppose you are running GCC manually otherwise you wouldn't have that problem.
you can run GCC for each .c file manually or you can just run it for them all togather.
gcc *.c
if you do the later, you should not run into linker errors.
You forgot include guards:
blank.h:
#ifndef BLANK_H_INCLUDED
#define BLANK_H_INCLUDED
bool blank(char string[]);
#endif
These include guards prevent the contents of the header file being redefined each time a source file includes it. Make sure to do this for boolean.h too.
You need to include boolean.h in blank.h,
// Header file for blank function
#include "boolean.h"
bool blank(char string[]);
or you need to include it befor blank.h in Test.c, otherwise the compiler doesn't know the type bool in the declaration of blank.
Apart from that, the advice to always use include guards is good and should be followed.
Once removing the #include "blank.h" from Test.c and running gcc Test.c blank.c it compiled fine. Thank you for the advice on the include guards, and doing gcc Text.c blank.c

Resources