-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How does include works in C and clang #29
Comments
Typically, C libraries are distributed as shared object binaries and header files. The shared objects contain compiled functions, and the headers contain forward-declarations for the functions in the shared objects. In any conformant C compiler, the This is supported by the fact that C allows multiple declarations (prototype alone) for the same function, but only one definition (prototype + body). |
For example, consider the following program, pgm.c: #include "pgm.h"
int main () {
fn1();
} with the following header file, pgm.h: int fn1 () {
return 10;
} This compiles to the exact same code (can be verified with a diff) as a single file with everything inline: int fn1 () {
return 10;
}
int main () {
fn1();
} And the emitted LLVM is: ; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @fn1() #0 {
ret i32 10
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @fn() #0 {
%1 = call i32 @fn1()
ret void
} |
Further, the only opportunity for type-checking lies with the frontend. The following code compiles with no error, not even a warning: //#include <stdio.h>
int putchar ();
int main () {
putchar();
} However, if the #include <stdio.h>
//int putchar ();
int main () {
putchar();
}
Note that in the first example, //#include <stdio.h>
int putcharr ();
int main () {
putcharr();
}
This supports 2 facts:
|
We need to parse C headers ourselves for linking then! We can use SWIG for parsing or we can use clang json output. |
Want to see how includes work in Clang and how we can directly include C libraries in c-lisp.
The text was updated successfully, but these errors were encountered: