Sharing functions between files in C
HTML-код
- Опубликовано: 18 янв 2022
- Source code can be found here:
code-vault.net/lesson/il55l1c...
===== Support us through our store =====
code-vault.net/shop
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-vault.net
Glad you're back! Thanks for doing it for us!:)
OH WOW! Are you back?? Nice! I am indebted to you man. You make such amazing content especially your threading and process videos have been life savers.
I saw your video and flash back to 80's when I learned my lovely C language. It could be great if you prepare a video "how to make your own lib in C".
Thanks for sharing an long live to C.
Beautiful explanation. Love it
Big thx from Moscow!
A thoughtful pedagoical video, because you show the files content, so it's much easier to understand your explanations.
When my projects have multiple files, I often wonder, if there exist such a tool, that could show a simple ascii chart with the different .c and .h files #includes. I would also appreciate if the tool could show the all the statics and global...
Hmm, you could look at Visual Studio or CLion, those IDEs might have something like this integrated
clear brief, Thanks
functions symbols have external linkage by default, that's why we were be able to compile the program without forward declaring the functions
great, a video about headers will be good!
For me best practice is to include files you need, the static function will be accessible but can't be overridden in subsequent files.
I get to header files in the next video. This was just to showcase how to actually share functions first. I feel like this order of explaining the concepts makes it easy to understand header files that are otherwise a complex topic
@@CodeVault Ok, you do an excellent job, btw.
Thank you!
You earned yourself a subscriber
thank you master
I noticed that when compiling larger, "professionally" written C projects, the strategy seems to be to compile every individual C file one at a time into an object file, and then do the linking into a binary at the end. In that context:
Is there a situation where you WOULD want to compile a couple of the C files together instead of compiling them one at a time?
That line I was using to compile all the files is, in fact, compiling each file one by one but also linking them together directly into an executable. It's just that it doesn't save those obj files to disk
@@CodeVault oh, I see. I did not realize that it was still doing them one at a time under the hood. Thank you.
By the way, many years ago I was experimenting with a GNU C compiler to try and understand the low-level details about the 4 stages of compilation:
1. Preprocess, using the -E flag with the 'gcc' program to get '*.i' files
2. Compilation, using the -S flag with the 'gcc' program to get '*.s' files
3. Assemble, with the 'as' program to get '*.o' files
4. Link, with the 'ld' program to get the final binary
and wrote a makefile that did these steps one at a time. When you invoke the compiler from the command line or run a makefile, it seems that normally you either jump directly to step 4--or you jump directly to step 3, and then to step 4. It was interesting to explore these first 2 steps and open them in a text/hex editor to see what was produced. I was able to compile this way for simple projects that only had a main.c file and nothing else, but I never got it to work properly with multiple C files for some reason. I was also doing this to try to understand why certain sections of code such as .TEXT were placed in certain areas of memory.
Anyhow, it would be cool someday to see a video series where you explore the guts of a C compiler like this.
My weird makefile is here for reference: github.com/yankee14/avr-makefile/blob/master/Makefile
Thanks! I'll keep this in mind and will make a video series about this later on. Certainly those steps are very interesting and answers a lot of questions people might have about the whole compilation process.
One interesting thing you could do, if you compile them step by step is you could link obj files together that were compiled from different languages (say C and Pascal). It's a very interesting concept that nowadays you don't see much of in modern programming languages.
@@CodeVault that is fascinating!
@ code valut
Can you explain what is volatile and why we need some advantages and disadvantage about volatile
I will look into it
We can also do this:
main1.c :
#include
#include"main2.c"
int main(){
printf("%d", sumOfTwoNumbers(32,3));
return 0;
}
main2.c :
int sumOfTwoNumbers(int a, int b){
return a+b;
}
run command line :
gcc main1.c -o main
./main
35
Yes, this works. But if you try to create another file (say utils.c), including main2.c inside and compiling it together with main.c you'll get an error since sumOfTwoNumbers will be defined in both main.c and utils.c
helpful
Hi
Can i ask you a really want to know if i can do a code for any differential equation that can user enter it i have to know it please help me 🥺🥺🥺
I think you can use gsl for that: www.gnu.org/software/gsl/
Can we take this one step further, and place these function prototypes inside their own .h files, so that we don't have to include all the headers inside our cpp files? How is it that we have to compile the cpp function code with the main.cpp, but default includes do not, such as , etc? Can we have it to where we can just say #include "myfuncs.h", and have it pull everthing it needs automatically?
Yes you can have a simple "myfuncs.h" file where you include everything you need
hey I use vs code(I am a beginner) and I don't have to write anything in my terminal to run the code, I simply click the run button......therefore is there a way by which I can use the function from other file without writing it into the terminal
You have to change the build task so it includes the right files (usually in tasks.json)
this may be off-topic but I'd love to see an in-depth comparison of C and Rust from your perspective
Could be an interesting video although I haven't used Rust too much myself. Thanks for the recommendation!
What about static global variables?
It's the same idea as functions. It makes them local to the file it's defined in
Can we share more than 4 function?
You can share as many functions as you want