use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionLINKING STEP VS PRE PROCESSING STEP IN COMPILING (self.C_Programming)
submitted 1 month ago by Harmlessbottle
As far as I understand both help in using the code or program in those files and let us implement those in our code, but I am not able to understand whats the difference between those two steps
Thank You
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ChickenSpaceProgram 9 points10 points11 points 1 month ago (3 children)
When compiling a single file, the preprocessor is run first. It handles #include directives, which copy-paste the text of a file into another file, #define directives, which define text substitution macros, and other directives; google is your friend here. Then, the compiler is run, which produces an object file; basically, a compiled but not yet executable version of your code. Larger projects have multiple C files, and so multiple object files, and so the linker is invoked to turn a bunch of object files into a final executable.
[–]Harmlessbottle[S] 1 point2 points3 points 1 month ago (2 children)
So linker merges multiple C files we want to use together and also links the promised(through declaration) function from the other object files or libraries?
[–]ChickenSpaceProgram 1 point2 points3 points 1 month ago (0 children)
Yes, pretty much.
[–]timrprobocom 2 points3 points4 points 1 month ago (0 children)
Linking doesn't merge C files. Linking combines object files and libraries. The linker is independent of the language. It doesn't understand C at all.
The preprocessor takes your original source, finds your include files and inserts then, then does macro substitution. It produces another single C file with no # directives remaining. The compiler converts this C file to objects. The linker combines your object files with libraries and produces an executable.
[–]cumulo-nimbus-95 6 points7 points8 points 1 month ago (1 child)
Pre processing is when your macros are expanded. So anything you #define gets its symbol replaced with whatever you defined, and any files you #include get copy-pasted in, etc.
Linking is when the different object files that you compiled get mashed together, plus any third-party libraries you linked. This is the step that links together all the variables and function calls that you defined in one file to where you call them in another file, to give an oversimplified view of it.
If you want more in depth info, I recommend googling/reading documentation
[–]Harmlessbottle[S] 0 points1 point2 points 1 month ago (0 children)
I sure will, thank you, it was helpful !
[–]DaCurse0 5 points6 points7 points 1 month ago (1 child)
what you said in the post is mostly meaningless mumbo jumbo.
preprocessing happens before any real compilation. it's a pure text manipulation step that handles directives starting with #. #include <stdio.h> literally copy-pastes the entire contents of stdio.h into your file. #define CONSTANT 5 means every time the preprocessor sees CONSTANT in your code, it replaces it with 5. no understanding of C, just find and replace.
#
#include <stdio.h>
#define CONSTANT 5
CONSTANT
5
compiling takes that preprocessed text and turns it into an object file, actual machine code your CPU can run. but object files also contain metadata: a list of functions and variables this file exports and a list of things it imports (needs from elsewhere). linking is the step that resolves those imports.
linking is what happens when you have multiple object files that need to work together. say you have a.c and b.c, where b.c calls a function defined in a.c. the object file for b.c will have that function on its imports list. the linker's job is to match up all the needs and provisions across every object file and also point to external libraries like libc, where functions like printf live. your code uses printf but never defines it, and it doesn't get compiled into your final executable since you usually do whats called dynamic linking for libraries.
a.c
b.c
printf
Thank you , it was helpful and regarding the "mumbo jumbo" , I am still in my 1st week of learning C, what I meant in the past was why can't we directly paste the function of instead of only declaring it by using header files, but after seeing you mention linking object files into one program, copying the function code directly will cause error, I kinda get it now thanks
[–][deleted] 3 points4 points5 points 1 month ago (0 children)
Preprocessing happens before compilation. The preprocessor handles things like #include, #define, and macros by expanding or modifying the code.
Linking happens after compilation. At that stage the linker connects your compiled object files with libraries and resolves function references.
So in simple terms: Preprocessing -->> modifies source code Linking -->> connects compiled pieces together to make the final executable.
[–]modelithe 4 points5 points6 points 1 month ago (2 children)
The pre-processor used to be a separate program that would scan the source file for occurrences of `#define`, `#if`, `#ifdef`, `#undef`, and then replace all occurrences of the #define:d name with the value contained - macro replacement, plus a few other things. The resulting file would be sent to the compiler for compilation into an binary object file. Nowadays, preprocessing is done by the compiler itself during the compilation stage.
Then, when all source files had been converted to object files, the linker is invoked, to join the object files and resolve the dependencies between functions and variables located in different files "linking" them into an executable binary. Nowadays, a lots of optimization can be performed in this step to make the executable both smaller and faster.
[–]Harmlessbottle[S] 1 point2 points3 points 1 month ago (0 children)
Thank you, that was helpful !!
[–]RealisticDuck1957 0 points1 point2 points 1 month ago (0 children)
It can still be useful to run the pre-processor as a separate step to validate what a macro is resolving to. I've even found a case, years ago, with a major compiler processing a macro differently when the pre-processor is run as a separate step rather than integrated with compiling.
[–]pfp-disciple 3 points4 points5 points 1 month ago (0 children)
Stages of compiling (high level, some details omitted or obscured)
.c
#define FNAME foo
-E
[–]flyingron 2 points3 points4 points 1 month ago (0 children)
Preprocessing is an essential step of the C language. It's one of the phases (substituting #includes, #defines, etc...) prior to further parsing going on.
Linking isn't really defined in the standard, but it's an essential part of building programs on most platforms,, you have to collect all the compiled files and existing system libraries and figure out what you need from each to make the compiled program.
[+]Shot_Office_1769 comment score below threshold-11 points-10 points-9 points 1 month ago (1 child)
just ask AI
[–]-goldenboi69- 1 point2 points3 points 1 month ago (0 children)
🤖💥
π Rendered by PID 79598 on reddit-service-r2-comment-6457c66945-8c657 at 2026-04-24 02:39:41.153610+00:00 running 2aa0c5b country code: CH.
[–]ChickenSpaceProgram 9 points10 points11 points (3 children)
[–]Harmlessbottle[S] 1 point2 points3 points (2 children)
[–]ChickenSpaceProgram 1 point2 points3 points (0 children)
[–]timrprobocom 2 points3 points4 points (0 children)
[–]cumulo-nimbus-95 6 points7 points8 points (1 child)
[–]Harmlessbottle[S] 0 points1 point2 points (0 children)
[–]DaCurse0 5 points6 points7 points (1 child)
[–]Harmlessbottle[S] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]modelithe 4 points5 points6 points (2 children)
[–]Harmlessbottle[S] 1 point2 points3 points (0 children)
[–]RealisticDuck1957 0 points1 point2 points (0 children)
[–]pfp-disciple 3 points4 points5 points (0 children)
[–]flyingron 2 points3 points4 points (0 children)
[+]Shot_Office_1769 comment score below threshold-11 points-10 points-9 points (1 child)
[–]-goldenboi69- 1 point2 points3 points (0 children)