Sharing functions between files in C

  Рет қаралды 28,758

CodeVault

CodeVault

2 жыл бұрын

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

Пікірлер: 38
@amyrs1213
@amyrs1213 2 жыл бұрын
Glad you're back! Thanks for doing it for us!:)
@SgtMajorJay
@SgtMajorJay 2 жыл бұрын
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.
@entropy79
@entropy79 2 жыл бұрын
Beautiful explanation. Love it
@juanmamani2110
@juanmamani2110 Жыл бұрын
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.
@selcukozen5486
@selcukozen5486 Жыл бұрын
clear brief, Thanks
@user-qs8kq2sq3s
@user-qs8kq2sq3s 2 жыл бұрын
Big thx from Moscow!
@user-yg6dt7qh6o
@user-yg6dt7qh6o Жыл бұрын
Thank you!
@parrotparrot1948
@parrotparrot1948 2 жыл бұрын
You earned yourself a subscriber
@grimvian
@grimvian Жыл бұрын
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...
@CodeVault
@CodeVault Жыл бұрын
Hmm, you could look at Visual Studio or CLion, those IDEs might have something like this integrated
@alpergunes6380
@alpergunes6380 7 ай бұрын
functions symbols have external linkage by default, that's why we were be able to compile the program without forward declaring the functions
@FretFury
@FretFury 2 жыл бұрын
great, a video about headers will be good!
@vilijanac
@vilijanac 2 жыл бұрын
For me best practice is to include files you need, the static function will be accessible but can't be overridden in subsequent files.
@CodeVault
@CodeVault 2 жыл бұрын
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
@vilijanac
@vilijanac 2 жыл бұрын
@@CodeVault Ok, you do an excellent job, btw.
@sulaimanatayi555
@sulaimanatayi555 Жыл бұрын
thank you master
@Yankee14ed
@Yankee14ed 2 жыл бұрын
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?
@CodeVault
@CodeVault 2 жыл бұрын
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
@Yankee14ed
@Yankee14ed 2 жыл бұрын
@@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
@CodeVault
@CodeVault 2 жыл бұрын
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.
@Yankee14ed
@Yankee14ed 2 жыл бұрын
@@CodeVault that is fascinating!
@ramakrishna4092
@ramakrishna4092 2 жыл бұрын
@ code valut Can you explain what is volatile and why we need some advantages and disadvantage about volatile
@CodeVault
@CodeVault 2 жыл бұрын
I will look into it
@haroldmcbroom7807
@haroldmcbroom7807 2 жыл бұрын
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?
@CodeVault
@CodeVault 2 жыл бұрын
Yes you can have a simple "myfuncs.h" file where you include everything you need
@hebamoh926
@hebamoh926 Жыл бұрын
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 🥺🥺🥺
@CodeVault
@CodeVault Жыл бұрын
I think you can use gsl for that: www.gnu.org/software/gsl/
@happybarsola8281
@happybarsola8281 8 ай бұрын
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
@CodeVault
@CodeVault 8 ай бұрын
You have to change the build task so it includes the right files (usually in tasks.json)
@surajmaity6194
@surajmaity6194 Жыл бұрын
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
@CodeVault
@CodeVault Жыл бұрын
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
@mehradtousinezhad1825
@mehradtousinezhad1825 Жыл бұрын
helpful
@suncrafterspielt9479
@suncrafterspielt9479 2 жыл бұрын
What about static global variables?
@CodeVault
@CodeVault 2 жыл бұрын
It's the same idea as functions. It makes them local to the file it's defined in
@jaimizan3782
@jaimizan3782 Жыл бұрын
Can we share more than 4 function?
@CodeVault
@CodeVault Жыл бұрын
You can share as many functions as you want
@atiabbz
@atiabbz 2 жыл бұрын
this may be off-topic but I'd love to see an in-depth comparison of C and Rust from your perspective
@CodeVault
@CodeVault 2 жыл бұрын
Could be an interesting video although I haven't used Rust too much myself. Thanks for the recommendation!
Global functions in multi-file projects in C
10:39
CodeVault
Рет қаралды 8 М.
100❤️
00:19
MY💝No War🤝
Рет қаралды 23 МЛН
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 13 МЛН
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 76 М.
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 20 М.
What are header files in C++ ( PROGRAMMING TUTORIAL for beginners)
23:54
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 74 М.
Multiple Files in C (Header and Source Files)
26:07
Jonathan Muckell
Рет қаралды 17 М.
Global variables in a multi-file project in C
8:42
CodeVault
Рет қаралды 16 М.
Difference between arrays and pointers in C
11:23
CodeVault
Рет қаралды 32 М.
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 379 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 338 М.
Short introduction to header files in C
13:34
CodeVault
Рет қаралды 81 М.