Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Senor Cuete

macrumors 6502
Original poster
I have a C .h header file with declarations like this:

enum foo {bar, barf};
enum brain {fart, fade};

When I #import "headername.h" into a .c file, the compiler recognizes the two foo enumerations, bar and barf, but not the brain enumerations, fart and fade - use of undeclared indentifier "fart" and use of undeclared indentifier "fade"

What's wrong?
 
I made a minimal test case.

File "fancy.h"
Code:
enum foo {bar, barf};
enum brain {fart, fade};

File "farty.c"
Code:
#include <stdio.h>

#include "fancy.h"

int main( int argc, const char * argv[] )
{
    enum foo fooish = barf;

    enum brain brainy = fade;

    printf( "Hello foo: %d \n", (int) fooish );
    printf( "Hello brain: %d \n", (int) brainy );

    return 0;
}

Cmds:
Code:
make farty
./farty

What version is your compiler? cc --version


I ran the test case on an old Snow Leopard Mac, and on a Raspberry Pi Zero-W connected via ssh. It worked on both.

The Pi's cc version info:
cc (Raspbian 8.3.0-6+rpi1) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
Last edited:
Now XCode has really gone off-the-reservation. It is OK with some .h and .c files but in some where I have #include or #import <stdio.h> it is reporting an error:

stdio.h expected identifier or '('

!@#$%^&
 
What source language is the "#import" written in?
Or put another way, what language is being compiled?

If it's C, then that's not a valid C directive. The correct one is "#include".
 
What source language is the "#import" written in?
Or put another way, what language is being compiled?

If it's C, then that's not a valid C directive. The correct one is "#include".
No, you are wrong. You can include a header file using either compiler directive - #include or #import - with slightly different results. If you use #import the compiler will only include the header in the project once. Look it up. XCode will import stdio.h using either one, just not in a couple of files, which I have removed from the project for now.
 
Maybe that feature of the compiler is broken, or some option was accidentally introduced that enables some kind of stricter C compliance.

Or maybe the rule for compiling those specific files differs from the rule for other files.

A test case might be to change a #import in a malfunctioning .c file to #include and see if it compiles.

Without actual source for the failing case, I'm out of suggestions.
 
I was wrong. I thought of something else.

Maybe there's an invisible character in the file, near the site of the error, and if it's not a whitespace character for the compiler, it's being parsed.
 
  • Like
Reactions: Greybeard2017
I was wrong. I thought of something else.

Maybe there's an invisible character in the file, near the site of the error, and if it's not a whitespace character for the compiler, it's being parsed.
thats what I came back to say - I'd try creating new files and typing the code from scratch

avoid cut and paste
 
thats what I came back to say - I'd try creating new files and typing the code from scratch

avoid cut and paste
The xxd cmd can produce hex dumps in various formats, and piped to grep to search for bytes in the range 0x80 and up, any unexpected values would become apparent.
 
I removed the bad files from the project, cleaned the build folder, and added the files back. Now it builds. The invisible character was a good guess.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.