Home > other >  Despite the header file being in the same directory as the program, C can't find the file
Despite the header file being in the same directory as the program, C can't find the file

Time:03-13

According to one of the books I'm reading,

When you write your own header files, use the second form of the preprocessor directive, the one that has quotation marks. When you use quotation marks, C first searches the disk directory in which your program is stored and then searches the built-in #include directory.

Program TestingGrounds.c

#include <stdio.h>
#include "TestingExtensions.h"

main()
{
    printf("%d", NUM);
}

Header file TestingExtensions.c

#define NUM 21

The directory for both files is MyFirstC.

The Error I get is, fatal error: TestingExtensions.h: No such file or directory.

It works just fine if I use #include "TestingExtensions.c".


I'm trying to imitate this example code:

// Example program #1 from Chapter 7 of Absolute Beginner's Guide to
// C, 3rd Edition
// File Chapter7ex1.c
/* This is a sample program that lists three kids and their school
supply needs, as well as cost to buy the supplies */
#include <stdio.h>
#include <string.h>
#include "Chapter7ex1.h"
main()
{

int age;
char childname[14] = "Thomas";
printf("\n%s have %d kids.\n", FAMILY, KIDS);
age = 11;
printf("The oldest, %s, is %d.\n", childname, age);
strcpy(childname, "Christopher");
age = 6;
printf("The middle boy, %s, is %d.\n", childname, age);
age = 3;
strcpy(childname, "Benjamin");
printf("The youngest, %s, is %d.\n", childname, age);
return 0;
}

In this example, for a file named Chapter7ex1.c, #include "Chapter7ex1.h" is used. I'm not sure what I'm missing.

CodePudding user response:

I would recommend double checking the spelling and capitalization of the .h file in the directory that you know it is in. It may be that there is a small difference between the two that is causing the compiler to be unable to see it.

CodePudding user response:

Check that your header file is called TestingExtensions.h exactly, including casing. You need to add a .h. extension to TestingExtensions

CodePudding user response:

Inserting a directive #include "TestingExtensions.h" in your source code does not tell the compiler to read the file TestingExtensions.c and treat it as a header. The file name used in the #include directive must be the same as the name of the file in the file system.1

When you want to share some things from a source file with other source files, the general practice is:

  • In a source file named SomeFileName.c, you define all functions and objects the source file provides to the program. Notably, you write functions with function bodies (the source code that implements the function), and you define objects with declarations such as int MyVariable = 3;.2

  • In a header file named SomeFileName.h, you declare the identifiers of functions and objects you want to make known to other parts of the program. Notably, you declare functions without function bodies, as in int MyFunction(int MyParameter);, where there is no { … } with the code for the function, and you declare variables without giving them initial values and often using extern, as in extern MyVariable;.

  • If the functions and variables you declare in the header file need special types, such as custom structures, you include definitions of those types in the header file.

From your comments, it appears you have a file named TestingExtensions.c that you are trying to include as TestingExtensions.h. You need to make a new file. Copy TestingExtensions.c to TestingExtension.h and edit it to make these changes:

  • Delete everything that you do not want to share with the rest of the program. For example, if the file contains functions foo and bar, and you only want to share foo, then delete the code that defines bar.
  • Replace all the function bodies (code in { … }) with ;, to change the function definitions to just declarations.
  • Change all the definitions of variables to declarations. Generally, you want to avoid having external variables, especially external variables that are shared between source files. However, if you do, change them to declarations by removing any initializers and prefixing them with extern.

Footnotes

1 A C implementation could translate header names from #include directives into different names in the file system. However, this was a feature that allowed the C language to be implemented in computers using file systems that treated names differently from how they appear in C source code, such as have limits on lengths or the cases of characters. In modern C implementations, the names in #include directives are almost always the same as the names in the file system, possibly with minor exceptions such as the character used to separate folders in paths.

2 Due to the history of how C developed, which declarations define objects and which are merely declarations that describe an identifier but do not define an object are complicated. To be sure, a declaration that provides an initial value as with = 3, defines an object. A declaration that uses extern and does not provide an initial value does not define an object. Some other forms may depend on context.

  •  Tags:  
  • c
  • Related