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

Комментарии • 38

  • @amyrs1213
    @amyrs1213 2 года назад +8

    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.

  • @juanmamani2110
    @juanmamani2110 10 месяцев назад

    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.

  • @entropy79
    @entropy79 2 года назад

    Beautiful explanation. Love it

  • @user-qs8kq2sq3s
    @user-qs8kq2sq3s 2 года назад +1

    Big thx from Moscow!

  • @grimvian
    @grimvian 11 месяцев назад

    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  11 месяцев назад

      Hmm, you could look at Visual Studio or CLion, those IDEs might have something like this integrated

  • @selcukozen5486
    @selcukozen5486 Год назад

    clear brief, Thanks

  • @alpergunes6380
    @alpergunes6380 5 месяцев назад

    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 года назад +1

    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 года назад +1

      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.

  • @user-yg6dt7qh6o
    @user-yg6dt7qh6o Год назад

    Thank you!

  • @parrotparrot1948
    @parrotparrot1948 2 года назад

    You earned yourself a subscriber

  • @sulaimanatayi555
    @sulaimanatayi555 Год назад

    thank you master

  • @Yankee14ed
    @Yankee14ed 2 года назад +1

    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 года назад +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 года назад +1

      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 года назад +1

    @ code valut
    Can you explain what is volatile and why we need some advantages and disadvantage about volatile

    • @CodeVault
      @CodeVault  2 года назад +1

      I will look into it

  • @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  Год назад +3

      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

  • @hebamoh926
    @hebamoh926 Год назад +1

    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  Год назад +1

      I think you can use gsl for that: www.gnu.org/software/gsl/

  • @haroldmcbroom7807
    @haroldmcbroom7807 Год назад

    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  Год назад

      Yes you can have a simple "myfuncs.h" file where you include everything you need

  • @happybarsola8281
    @happybarsola8281 6 месяцев назад

    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  6 месяцев назад

      You have to change the build task so it includes the right files (usually in tasks.json)

  • @atiabbz
    @atiabbz 2 года назад +1

    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 года назад +2

      Could be an interesting video although I haven't used Rust too much myself. Thanks for the recommendation!

  • @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