Changes in included file not registering in C

1.4k views Asked by At

I am on Arch Linux. I have tried gcc and cc.

I have quite a strange problem. I have a file included from /usr/include (installed from an Arch package) in a C program like so.

// prog.c
#include <foobar/foobar.h>

When I change it, nothing happens. Let me explain. It includes some C code.

// foobar/foobar.h
int baz = 1, qux = 2;

Recently, it has been updated.

// foobar/foobar.h
int baz = 1, qux = 2, norf = 3;

My test program looks something like what follows.

// prog.c
#include <foobar/foobar.h>
printf ("%d %d %d\n", baz, qux, norf);

output:

error: ‘norf’ undeclared (first use in this function)

I can duplicate the file in the same directory, name it foobar2.h, and then include that file instead and it outputs:

1 2 3

So the path is not incorrect. I can make a link to the folder, name it foobar2, and include foobar2/foobar.h and it outputs:

1 2 3

So the file is not incorrect.

To confirm that it is not registering changes, I can destroy the foobar folder entirely and try to print just the 2 variables that were originally inside the header. This outputs:

1 2

Clearly something is not updating. The same behaviour is displayed when I try updating the file with enums, functions, or new values for existing items in the file, and include them as the only lines in the file. None of the changes register. The same activity is displayed with gcc and cc. This has been going on a few months now and it still has not resolved itself.

2

There are 2 answers

2
rbong On BEST ANSWER

When a file does not seem to be updating, it is a good idea to check other places on your system where your compilers search for libraries for identically named folders. In this case, it was in /usr/local/include, where I had a version that I compiled and forgot about entirely.

0
Karthik Balaguru On

This can be mainly due to the include directory path mismatch. Check out the environment variable that can affect the behavior like C_INCLUDE_PATH , CPATH. Another option would be to check with gcc -I/src/directory foobar.h while compilation that can lead you closer to the problem or use #include "foobar.h" in the code.

In case if there are multiple copies of the program in your system, it is possible that completely different file is used for compilation and execution which can also lead to changes not getting reflected.