Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Include .h not working?

Tags:

c

gcc

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;
like image 583
amedeiros Avatar asked Dec 06 '25 14:12

amedeiros


2 Answers

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.

like image 55
user657862 Avatar answered Dec 08 '25 07:12

user657862


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.

like image 38
user1708860 Avatar answered Dec 08 '25 06:12

user1708860



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!