Header Issues: Guards, Name Mangling, and extern "C"

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 เม.ย. 2019
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.thinkific.com
    Website ➤ www.jacobsorber.com
    ---
    Header files often have things in them that confuse beginning programmers. This video explains two of them - ifdef guards and extern "C" blocks.
    Objdump video: • How to Inspect Compile...
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    www.jacobsorber.com
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
    Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]
    Want me to review your code?
    Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
    You can also find more info about code reviews here.
    • I want to review your ...

ความคิดเห็น • 125

  • @Minty_Meeo
    @Minty_Meeo 2 ปีที่แล้ว +81

    6:44
    _Z prefixes all Itanium mangled symbols to make it abundantly clear that the symbol name is mangled, else certain names (global variables) can be impossible to tell if mangled or not.
    7 is the string length of "getmean"
    Capital P is short for pointer, i is short for int, d is short for double.

    • @adamvalt6609
      @adamvalt6609 2 ปีที่แล้ว +4

      that makes so much sense!

  • @SteelHorseRider74
    @SteelHorseRider74 4 ปีที่แล้ว +55

    In your videos every aspect of C programming looks so clear and straight forward - in my C code things just segfault and dump... :D

  • @lean.drocalil
    @lean.drocalil 3 ปีที่แล้ว +30

    Wow! It really makes me stand in awe to see how well you can explain stuff and cram this much of excellent content into one succinct and beautiful video.
    Congratulations!! 👏🏻👏🏻

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +2

      Thanks, Leandro. Glad you enjoyed it.

    • @anshul493
      @anshul493 3 ปีที่แล้ว

      @@JacobSorber gcc-compiler is aliased to clang-compiler in the mac
      the manual page of gcc (it doesen't exist)
      see this: % gcc --version

  • @joseraphaelmaglunsod7548
    @joseraphaelmaglunsod7548 4 ปีที่แล้ว +6

    Thanks for this video Jacob, I understand now the used of header guards. 😊

  • @renatac210
    @renatac210 5 ปีที่แล้ว +21

    Your videos are so great! Thank you for your work!

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      Thanks, Renata. Glad you're enjoying them.

  • @ahmetelhaddadi6822
    @ahmetelhaddadi6822 4 ปีที่แล้ว +1

    Unbelievably useful and EASY TO UNDERSTAND.Bit respect,sir

  • @theshaswat
    @theshaswat 4 ปีที่แล้ว +4

    Millions and millions of thanks for such a beautiful explanation in such a short video. Never heard "Name Mangling" before.
    Please keep up the good work👍👍

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      You're most welcome.

  • @johnjoyce
    @johnjoyce 2 หลายเดือนก่อน

    Excellent delivery and a great topic for C, C++ and Objective-C compilation

  • @allochi
    @allochi 4 ปีที่แล้ว +1

    These are amazing videos! keep up the good work!

  • @parthpanchal3843
    @parthpanchal3843 2 ปีที่แล้ว

    Very much informative, loved it!!. Keep making more like this.

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq 3 ปีที่แล้ว

    Awesome explanation. Crisp and precise! Thanks.

  • @coenraadmulder
    @coenraadmulder 3 ปีที่แล้ว

    Thank you very much, this is the best explanation i have seen, if you understand a concept you will never forget. Well explained. Thank you

  •  5 ปีที่แล้ว +20

    I know it's not common, but I always indent #ifndef, #define, #endif etc. Makes the code a lot easier to read right away. I can't see why one should not indent these guards. Jacob, thank you for such great videos. :)

    • @AbhishekBM
      @AbhishekBM 3 ปีที่แล้ว

      Sometimes when you are dealing with a huge block of code, not indenting them makes the code a little tidier.

    • @MaxCE
      @MaxCE 2 ปีที่แล้ว +2

      if the ifs are on the start and end of file then no, but if it's inside a function then I do indent

    • @scottfranco1962
      @scottfranco1962 ปีที่แล้ว +8

      Because older C preprocessors didn't recognize the "#" at the start unless it was the first character on the line.

    • @dmitripogosian5084
      @dmitripogosian5084 ปีที่แล้ว +3

      I actually do not indent them because they are not part of the code flow, and often, as said encompass large section of code, if not the whole file

  • @samplesandtests
    @samplesandtests 2 ปีที่แล้ว +3

    besides a college course on C++ i don't think i have used function overloading. I had assumed that both C and C++ both supported it. this is the problem with taking a C++ course with out a C course or a C++ course making the assumption that the student will only use c++ and never C. thank you i learned something new today

  • @alex_8704
    @alex_8704 4 ปีที่แล้ว +1

    The most perfect video on the subject

  • @Microbert90
    @Microbert90 ปีที่แล้ว

    Wow thanks alot! Stumbled about an issue on a large mixed c/c++ project at work today where the name mangling causes some errors I couldn't explain in the first place. Then a colleague told me about the name mangling and this video came in really handy!

  • @bandisandeep
    @bandisandeep 5 ปีที่แล้ว +1

    Great explanation! Thanks

  • @mikelemon5109
    @mikelemon5109 4 ปีที่แล้ว

    Well I think you are realy onto the best ways to teach programming teaching the real fundamentals you actually need to move on explaining purposes and showing why some parts necessary in a subtructive manner good job help spread this so needed teaching model!

  • @notyetdecided
    @notyetdecided 4 ปีที่แล้ว +1

    awesome explanation with in less time. thanks a lot Jacob 😊😊😊❤❤

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +1

      My pleasure! Glad it was helpful.

  • @josephstark758
    @josephstark758 5 ปีที่แล้ว +2

    This is a great place, u got a new subscriber

  • @pathayes2224
    @pathayes2224 3 ปีที่แล้ว

    Yet again an excellent explanation

  • @harishkantsoni9977
    @harishkantsoni9977 3 ปีที่แล้ว

    Thanks for the information. Keep uploading new videos.

  • @starc0w
    @starc0w ปีที่แล้ว

    Awesome explanation! Thank you so much!

  • @jgcooper
    @jgcooper 3 ปีที่แล้ว

    Hey, I've been loving your videos, do you have any explaining "extern inline" and percompiled headers?

  • @raym6415
    @raym6415 3 ปีที่แล้ว

    Beautifully explained.

  • @pramoth-dev
    @pramoth-dev 3 ปีที่แล้ว

    Wow! I read it a lot but never understand it until I found this video. Awesome. Thank you.

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      You're very welcome!

  • @samvelabrahamyan7230
    @samvelabrahamyan7230 5 ปีที่แล้ว +1

    Two days ago I was trying to test my C code with Boost testing library, but I gave up without understanding the reason. Now everything makes sense :D Thanks

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      Nice. Glad it helped.

  • @jimzhu7654
    @jimzhu7654 5 ปีที่แล้ว +1

    thank you for this great video

  • @santoshkandarkar5442
    @santoshkandarkar5442 3 ปีที่แล้ว

    Hey Jac, simple is awesome. Nice video.

  • @birukberhanubekele3839
    @birukberhanubekele3839 4 ปีที่แล้ว +3

    Thank you sir, please don't stop creating good contents like this

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +1

      I'll keep doing what I can. Thanks for watching. Please spread the word.

    • @birukberhanubekele3839
      @birukberhanubekele3839 4 ปีที่แล้ว

      @@JacobSorber I really will (~_~), Thanks

  • @whirvis
    @whirvis 2 ปีที่แล้ว +4

    6:51 This is just a guess, but I believe the __Z17 in print_person_info and the __Z7 in getmean represent the length of the original function names 😁

  • @kelvintakyi-bobi3155
    @kelvintakyi-bobi3155 ปีที่แล้ว

    Thank you, Jacob☺️

  • @shadyganem5448
    @shadyganem5448 3 ปีที่แล้ว

    great video. won my subscription fair and square

  • @ashoksahu1926
    @ashoksahu1926 4 ปีที่แล้ว +1

    Awesome. Thank you

  • @47Mortuus
    @47Mortuus 8 หลายเดือนก่อน +1

    5:57
    a) The return value is NOT part of the function signature
    b) "arguments" are not part of the function signature - parameters are (arguments are "instances" of parameters)
    Being painfully careful about clear communication is key in CS, especially when teaching beginners

  • @rahulr9301
    @rahulr9301 2 ปีที่แล้ว

    a big wow!!!! amazing!!! i paused the video hit like and subscribe and the resumed the video!!! Thanku so much for your contribution, through youtube

  • @lordangus77
    @lordangus77 4 ปีที่แล้ว

    Hi Jacob! you're so great teaching C code. I love your videos!!
    I've been working with java (more recently kotlin) and android for 4 years now. The thing is that I'm tired of it and I definitely want to try smth different. I'm looking for C and Rust as languages of my interest, so can you please give me some advice about what should I do to start getting my hands dirty and learn real world C code??
    Thank you so match and keep the great work :)

  • @hsaidinsan6345
    @hsaidinsan6345 4 ปีที่แล้ว

    As always top content

  • @pipaliyakuldeep7487
    @pipaliyakuldeep7487 2 ปีที่แล้ว

    Useful information

  • @Sahilbc-wj8qk
    @Sahilbc-wj8qk 5 ปีที่แล้ว +2

    plz next video on Linker how it work... and able to figure out and connect code in libs to our code ??Thank you for these amazing video i learn every time new from these videos.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +3

      Thanks for the request. I'll add it to the list and see what I can do.

  • @waltwilliams6012
    @waltwilliams6012 ปีที่แล้ว

    One thing you could touch on is why some header includes have a forward slash in them -->

  • @spaceinvader8892
    @spaceinvader8892 3 ปีที่แล้ว

    That's cool. Thank you !

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      You're very welcome.

  • @dgholstein
    @dgholstein ปีที่แล้ว

    Great video and subject.
    I found out about extern C when creating a DLL to be used by LabVIEW, an interpreter. The same issue would exist with any extensible interpreter, imagine VBA and Python calling DLLs written in C++.

    • @CoolKoon
      @CoolKoon 7 หลายเดือนก่อน

      Actually it IS possible to call C++ code from within Python, in fact there are multiple ways of doing it.

  • @TheJustinChacko
    @TheJustinChacko ปีที่แล้ว

    Thank you. 👏

  • @marlene5547
    @marlene5547 3 ปีที่แล้ว

    I love your channel :)

  • @anshul493
    @anshul493 3 ปีที่แล้ว +1

    hey hey:
    gcc is aliased to clang in macOS
    try the manual page of gcc (it doesen't exist)

  • @danielkrajnik3817
    @danielkrajnik3817 2 ปีที่แล้ว

    brilliant

  • @tastyham
    @tastyham 2 ปีที่แล้ว

    so basically it makes the getmean() function not "depends" from the rest of the code and can therefore be used as a dynamic library?
    I've created some code that does that but to explain it, well it's hard to find any explaination.

  • @chintusharma1
    @chintusharma1 3 ปีที่แล้ว

    Sir can you make a video on parser and it's types .how to make a simple parser but not the compiler parser

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      Yeah, probably. What do you want to parse?

  • @davidgillies620
    @davidgillies620 6 หลายเดือนก่อน

    You can't overload by return type alone on C++. The parameter lists (perhaps after a bit of meta/SFINAE stuff) must be distinct.

  • @zxuiji
    @zxuiji 2 ปีที่แล้ว

    1:53, yeah I don't normally use extern C directly, instead I just set a few macros like so:
    #ifdef __cplusplus
    #define LINK_C extern "C"
    #define OPEN_C LINK_C {
    #define SHUT_C }
    #else
    #define LINK_C
    #define OPEN_C
    #define SHUT_C
    #endif
    Then just use 'em instead, less code overall

  • @andrewnorris5415
    @andrewnorris5415 ปีที่แล้ว +1

    I've download an open source project I want to work on called Treesheets. All the code is in the header files. I've modified some of it and added new features. BUT I still cannot work out why the heck he put the code in the h files. Will be looking into it so I fully understand it. There has to be a reason. He works for google and knows what he is doing. I may email him if cannot find out myself as he is open to emails, but want to research myself first!

  • @moralz_official7608
    @moralz_official7608 5 ปีที่แล้ว +1

    please i am looking at learning a language that is multidimensional as in can let me cut accross robotics,arduino,software,embedded systems etc without being limited but will love to know te best language to learn

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      Some of my thoughts on languages can be found here.
      th-cam.com/video/wcpH2PkC-94/w-d-xo.html

  • @xeridea
    @xeridea 2 ปีที่แล้ว

    Instead of standard header guards, you can just use #pragma once. You don't need to worry about duplicate guard names, and it is a bit cleaner looking. It is possible if you use symlinks, it can fail, but compilers also check for filesize, hash, etc so it is unlikely.
    Also... C doesn't support overloading? Didn't know that.

    • @123creator7
      @123creator7 2 ปีที่แล้ว +8

      #pragma once isn't part of the C standard, so it may not compile on some compilers. Compiler-dependant behavior isn't a good thing.

  • @mr3zzoz
    @mr3zzoz 10 หลายเดือนก่อน

    I have a question let’s say we have a project . When do we use c and when its c++?

  • @didoxgatete6338
    @didoxgatete6338 5 ปีที่แล้ว

    can you please explain to me the relationship between pointers and processor architecture
    and what datatypes in FILE structure

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +1

      Pointers are data types that store memory addresses. The size of those addresses is certainly defined by the processor architecture (some machines have 16-bit addresses, some 32, and some 64). As for the FILE structure...I'm really not understanding the question. Are you referring to the FILE * type that's returned from fopen()?

    • @didoxgatete6338
      @didoxgatete6338 5 ปีที่แล้ว

      still some little bit confusion does those bits means the size of a pointer?
      when i dereference the pointer with an int data type it give me 4 access block in decrementing mode like this:*(p-1),*(p-3) why?
      sizeof(FILE*)=48;how those it read that such big data not on RAM but on hard disk..again thx a lot..i appreciate.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      @@didoxgatete6338 I'm sorry. I don't understand your question.

    • @geiger21
      @geiger21 4 ปีที่แล้ว

      @@didoxgatete6338 file struct does not contain the contents of the file. It probably just contains a pointer to a memory to which the file's content was loaded. Size of struct must be known, but when you read a file you don't know how big is that so the only way to "store" the content of a file in a struct is to give a pointer pointing to memory that has been read from the file. This is why FILE* is always some fixed size, not depending on file size.

    • @xCwieCHRISx
      @xCwieCHRISx ปีที่แล้ว

      @@geiger21 The FILE struct implementation is compiler dependent. More likely dependent on the implementation of the standard library which is dependent on the OS. There is a reason why there is gcc and MinGW (windows gcc port). The compiling process is almost the same, but the standard library isnt. One uses linuey system calls the other uses windows api. The standard library gives you standardized interface. But how it is working under the hood is more like blackbox and shouldnt be your concern. You can read the open source implementation of the std library if you really want to know. But its dependent on your OS.

  • @moralz_official7608
    @moralz_official7608 5 ปีที่แล้ว

    hello sir,i presently i'm an electronic and electrical engineer and i am so passionate about programming however plc's ,logic controllers are queit a llittle not that.however i will love to know if learning c in 2019 could still be a relevant programming language for me and what is the possibility that it is multi fonctional in todays tech demand interms of software dev,mobile app dev etc thanks for your response

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      Short answer is, Yes, I think C is still a very relevant language. Long answer is partially contained here. th-cam.com/video/wcpH2PkC-94/w-d-xo.html

  • @josephstark758
    @josephstark758 5 ปีที่แล้ว

    Pls make a video on how to embed python in c/c++ code

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      Just to clarify. You want to call python code from C/C++ or the other way around?

    • @josephstark758
      @josephstark758 5 ปีที่แล้ว

      @@JacobSorber yeah I want to call python from c/c++ code

  • @novak6882
    @novak6882 2 ปีที่แล้ว

    U r awesome

  • @arielspalter7425
    @arielspalter7425 3 ปีที่แล้ว

    I thought it was impossible to overload a function based on return value only. . I suppose I was wrong

  • @necatiergin
    @necatiergin 3 ปีที่แล้ว

    double getmean(const double *, int length);
    void print_person_info(const person *);

    • @Kefford666
      @Kefford666 2 ปีที่แล้ว

      Couldn’t it even be:
      const double * const values
      and
      const person * const p
      Because they’re pointers to values and neither the pointers or the values will be modified by the function?
      But are you saying for pointers you don’t need a name? Or don’t need a name for the declaration but do for the definition?

    • @Kefford666
      @Kefford666 2 ปีที่แล้ว

      I don’t think this was meant to be the focus of the video though

  • @atik7
    @atik7 5 ปีที่แล้ว +3

    How about #pragma once? much simpler than defined :-)

    • @DenisPetrovSenior
      @DenisPetrovSenior 5 ปีที่แล้ว

      Assumed that pragma once is not supported by all of the compilers and is not a part of C standard.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +4

      It's definitely a good option. #pragma once isn't part of the standard, but it's well-supported by a lot of compilers.

    • @animeprogrammer
      @animeprogrammer 4 ปีที่แล้ว

      @@JacobSorber even then if you try to use more twice it'll produce the same error

  • @trevortrevose9124
    @trevortrevose9124 ปีที่แล้ว

    I love how he chooses to zoom In rather than expand his terminal 😂

  • @imancs
    @imancs 4 หลายเดือนก่อน

    👍

  • @axlslak
    @axlslak 4 ปีที่แล้ว

    Is that gnome builder?

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      No. It's either atom or vs code. Can't remember off the top of my head.

  • @scottfranco1962
    @scottfranco1962 ปีที่แล้ว

    And it deserves to be mentioned: Putting code in a header file SHALL BE PUNISHED!!!

  • @hectorcanizales5900
    @hectorcanizales5900 2 ปีที่แล้ว

    7:15 ah I C what you did there haha

  • @Gojam12
    @Gojam12 ปีที่แล้ว

    Programming music is quite similar to porno music, not that I know first hand. Thanks for sharing your expertise

  • @bhagyashingale5663
    @bhagyashingale5663 2 ปีที่แล้ว

    #pragma once does the same?

  • @XenoTravis
    @XenoTravis 5 ปีที่แล้ว

    Do you do Skype lessons? I could use some pointers lol.

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +1

      Nice. :)
      I don't. Too much to do, not enough time. But, if you have a specific request, I can look into making a future video on it.

    • @XenoTravis
      @XenoTravis 5 ปีที่แล้ว

      @@JacobSorber have you done one on smart pointers?
      Or threads? I am not sure if you stick to c or will do a c++11 video.

    • @XenoTravis
      @XenoTravis 5 ปีที่แล้ว

      Also. It would be pretty cool if you have a mini assasignment at the end. Then post the code you wrote after a day or two. I always need some practice on the basics to make sure I am using them effectively

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว +2

      @@XenoTravis I have some threads videos. I'll add smart pointers to the list. I tend to focus on C because my operating systems class uses C, and I do a lot of C programming in my research. But, I'm definitely open to other languages.

    • @XenoTravis
      @XenoTravis 5 ปีที่แล้ว

      @@JacobSorber sweet. C++ would make the most sense if you do another type of language. Before you do, it would be nice to have a video on the pros and cons of each.

  • @antonvagen8206
    @antonvagen8206 2 ปีที่แล้ว

    Ever heard of pragma once?

  • @minhajsixbyte
    @minhajsixbyte 8 หลายเดือนก่อน

    name mangling. cool

  • @bastawa
    @bastawa 4 ปีที่แล้ว

    Goldmine

  • @joebravo4224
    @joebravo4224 4 ปีที่แล้ว

    #pragma once

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +2

      Yes, that usually works, too. It's not required in the standards, but most compilers support it. Thanks.

  • @georgecop9538
    @georgecop9538 2 ปีที่แล้ว

    I've come back just to say #PRAGMA ONCE

  • @polluks2
    @polluks2 2 ปีที่แล้ว

    Don't use $(CPP) but $(CXX)

  • @soreachsina1472
    @soreachsina1472 5 หลายเดือนก่อน

    ayoo is that a KHMER DICTIONARY in the background

  • @andrewnorris5415
    @andrewnorris5415 ปีที่แล้ว

    Just checked the source code for stdlib.h and it is indeed contained within "#ifndef _STDLIB_H" - and - " #endif /* stdlib.h */"

  • @IK-ow2zk
    @IK-ow2zk 2 ปีที่แล้ว

    Thanks a lot!