Reputation:
I have a question, but I'm not sure if it's about C, clang or OSX.
When I compile a simple GLUT program like so:
clang test.c -framework OpenGL -framework GLUT
And I intentionally insert a function call that doesn't exist, like so:
thisFunctionIsNotDefinedAnywhere();
I get an error, as expected. But here's the rub -- I don't get the error until link time!
Undefined symbols for architecture x86_64:
"thisFunctionIsNotDefinedAnywhere" referenced from:
_main in test-e099d2.o
ld: symbol(s) not found for architecture x86_64
Why is this? Is this because pre-C99 there were implicit declarations? I've been programming for a long time and have never run into this before. Is it because I've been spoiled on GCC
and MSVC
which (I seem to remember) cause a compiler error in this situation? Or does it have to do with how framework linking works in OSX, which I am new to?
I appreciate any clarification!
Upvotes: 1
Views: 239
Reputation: 54562
As expected, this gives a warning with a recent version of clang. I tried it with the version that comes with Xcode 6.1, and got this compiler output:
$ clang test.c
test.c:2:5: warning: implicit declaration of function 'thisFunctionIsNotDefinedAnywhere' is invalid in
C99 [-Wimplicit-function-declaration]
thisFunctionIsNotDefinedAnywhere();
^
1 warning generated.
Undefined symbols for architecture x86_64:
"_thisFunctionIsNotDefinedAnywhere", referenced from:
_main in test-376416.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Since using undeclared functions is legal in C, the compiler message can't be an error. But it does show a clear warning, without specifically enabling warnings with command line options.
Upvotes: 1
Reputation: 53006
Because of implicit function declaration, if you enable warnings then you would be warned that thisFunctionIsNotDefinedAnywhere();
is implicitly declared, returning int
by default.
If you add a function prototype, then the warning will be gone, but then at the link stage, the compiler wont find the function definition, and issue the error.
Upvotes: 0