compilation - Need some advice on C header inclusion -
introduction
i'm experienced programmer , have years of experience object-oriented paradigm. lately i've decided try , more familiar , comfortable languages bit lower-level languages such c# or java, i'm delving c, , attempt create games it.
everything going pretty well, , i'm attempting organize code better moving function definitions separate .c files. start getting uncomfortable, due nature of inclusion in c. i'm quite familiar how works, , preprocessing stage during compilation. problem i'm still not sure i'm dealing correctly.
i think problem resembles has been asked here. though did not satisfy need know.
the problem
essentially, i'm creating opengl project using 2 libraries. 1 glew (for opengl function loading), , other glfw (for window handling , opengl context creation). have 2 .c files, 1 called main.c , called windowinitialize.c. problem is, both main.c , windowinitialize.c depends on glew , glfw libraries.
in main.c file following includes:
#include "includes/glew/glew.h" #include "includes/glfw/glfw3.h" #include "windowinitialize.h"
furthermore, main.c following relevant function calls:
if (!initializeglfw()) return -1; glfwwindow *window = createopenglwindow(3, 3, 640, 480, "hello opengl"); /* important initialize glew after initializing glfw , setting opengl context, glew needs current context work */ initializeglew();
windowinitialize.c includes definitions functions initializeglfw , initializeglew, createopenglwindow. main.c source though still uses both glew , glfw main loop purposes, referring functions , such both.
now, windowinitialize.c these includes:
#include "windowinitialize.h" #include "includes/glew/glew.h" #include "includes/glfw/glfw3.h" #include <stdio.h>
then proceeds fill out function definitions. there's deal. both files need make use if glew , glfw libraries, stdio (for printing debugging messages , such).
i feel isn't right way it, i'm not sure. believe reason compiler/linker not complaining double declarations because main.c , windowinitialize.c gets individual object code files.
my question though, linker clever enough put these declarations in executable file once? though include these headers several places, won't have effect on file size , such? , correct way of handling common dependencies between several source files? or should use approach?
everything you've described seems normal way things. 2 compilation units getting same forward declarations of functions in headers, not actual duplicate definitions libraries.
my question though, linker clever enough put these declarations in executable file once?
the linker doesn't care header files include - it's linker, not compiler.
so though include these headers several places, won't have effect on file size , such?
extra/unused forward declarations of functions have no effect on binary size.
and correct way of handling common dependencies between several source files?
yes, seem doing things in normal way.
for project, should doing like:
cc -c -o main.o main.c # compile main.c cc -c -o windowinitialize.o windowinitialize.c # compile windowinitialize.c cc -o myprogram main.o windowinitialize.o -lglew -lglfw # link objects necessary libraries create executable
but, really, using makefile better:
myprogram: main.o windowinitialize.o cc -o $@ $^ -lglew -lglfw
example of using it:
$ make cc -c -o main.o main.c cc -c -o windowinitialize.o windowinitialize.c cc -o myprogram main.o windowinitialize.o -lglew -lglfw
Comments
Post a Comment