Using Libraries in C++ (Static Linking)
Vložit
- čas přidán 29. 08. 2024
- Patreon ► / thecherno
Twitter ► / thecherno
Instagram ► / thecherno
Discord ► thecherno.com/...
Series Playlist ► thecherno.com/cpp
Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K
Would love to see a video that explains "Make" files and using things such as CMake
That would be great, "Makefiles" are pretty complicated imho
@@groberti 100%
@@groberti Especially for beginners
I'm agree!! what if we don't use visual studio?
Would be great see how to do this with cmake
Personal Notes:
- There are two parts in a library usually: includes and libraries. İnclude directory has a bunch of header files and lib directory has those pre-built binaries.
- Dynamic library is loaded(linked) into program at runtime, dynamic library can be anywhere and you do the linking whereas static library is put(compiled) into your executable program
- We have to point our compiler to header files (include files) and then we also have to point out our linker to library files.
And don't forget the library names :P
0:32 "its pretty simple!" said no CMake user ever!
@Spixmaster Pain in the ass for real
Lmao I feel you
...i just spent 2 hours messing with CMake. right in the feels
Make > CMake
I know I've probably already said this 100 times but thank you so much for all of your videos, they seriously help me so much. A lot of people who program aren't the best writers lol so it's really hard for me to follow along with most textbooks and written content from MIT, but watching you and hearing your clear explanations and SEEING how things work is so, so helpful, and makes it easier for me to code along with your examples (and pause the video to experiment if I have a "What if I do this?" moment) and to have it stick in my mind better
I'd be interested to see the "importing source code into another project in the same solution and compiling it to a static library" video.
redchant I second that!
redchant I third that
redchant I fourth that!
redchant I fifth that
I need this right now
1:37 watching on a linux platform, a windows sound echoes
wtf?
he probably had his audio inputs a little messed up while he was recording the video, so his system audio got routed into the audio track in his video software :P
go home drunk, you are linux
yeah lol
lol
lol, yes
Coming from a GNU background I always found Visual Studio projects confusing to setup. This video cleared a lot of my confusion about using external libraries. Thanks!
Yeah, dealing with IDEs is always such a mess... the syntax highlighting is nice though. I find things like Sublime to be a nice middle ground.
nothing clear than that.
the best c++ channel ever.
I love the idea of explaining with eye contact.
Cherno, you are the only person that has made this make sense. You're an incredible teacher.
+1
Wow this is amazing!!! I spent more than 8 hours trying to create libraries using the GLFW source code and Cmake. This was so much simpler, and he actually explained how the linkers work. Kudos to you man !!!
Anant Jain same ahaha
you find this simpler than using CMake? You’re out of your mind
@@mario_luis_dev No
great video. It often feels like so many other resources assume that one has this knowledge "a priori" and then you are stuck. You've shown that it is not that difficult, but one needs to learn it.
1:37
Windows wants to tell you something.
no sound drivers, makes sense hehe
I thought it was the sound of my computer :>)
@Andrew Alfazy you didn't hear the ba dum tsss.
@Andrew Alfazy in plain 2018 Linux still hasn't got stable NVida support
You heard it too, hm?
man… Windows is such a clunky OS. This would’ve been a million times cleaner if it had been done on either Linux or MacOS
Very good video!
For the Linux users -
Static lib (".lib") is ".a" (Archive) in Linux and dynamic lib (".dll") is ".so" (Shared Object).
For MacOS dynamic libraries have the .dylib extension btw
I've been going through this series one video at a time, and it feels so rewarding to finally be covering libraries!
Nobody at university shows you this stuff. If I weren’t proactive in my studies I’d still be stuck writing cli programs. We’ve covered 3 languages and no one’s touched upon the topic of third party libraries. The closest we’ve gotten to an explanation was “just do ‘pip install’ and the manager will handle it” (python). No mention on basic stuff like having ‘requirements.txt’ or using virtual environments (especially on Linux).
So in conclusion: Thanks for the awesome videos! Really helpful
Great stuff as always , and yes linking libs from source code video would be a nice add to this series
this is the crap that hindering me from using c++ for years...i basically stuck with console apps...
@@SJKKA GL 😂👌
@@SJKKA use vs code, it is more transparent, you will learn a lot cuz you have to do it manually using .json files but it takes lot time. It took me 4 months.
@@xyz8206 How do you do it with .json files. Can you link an article or something?
Thank you for going in depth. I watch all your videos to the end.
i too they are simple and in depth
people who judge you for waving your hands, have never been in a position to see themselves in action. i put up a mirror at work so people couldn't surprise me. i knew i was a bit of a cartoon character before that. now i'm sure.
hello, I am enjoying your tutorials, but at 12:10 the include is showing me an error saying "cannot open source file GLFW/glf3.h" any help would be appreciated. the compiler error is C:\Users\Name\Desktop\CppDev\projects\ProjectSol\Project1\test.cpp(5,9): fatal error C1083: Cannot open include file: 'GLFW/glfw3.h>': No such file or directory
It's not that Mac/Linux particularly love building code, though some people do really just like compiling everything from source for whatever reason that suits them. In which case the statement is accurate. There are other common cases to consider. I'll elaborate with a few examples.
It's often the case (in some fields) that you will be targeting a different architecture and are building an executable for another computer to run. You can be building a project for proprietary hardware like an embedded system. This one is pretty easy to understand, your computer has a lot of extra stuff compared to, say, a smart lightbulb. Or manufacturing equipment that makes paperclips using various sensors and actuators.
Even traditional application development runs into issues, now that some 32-bit architectures are being deprecated. MacOS stopped supporting 32-bit architectures completely. Some libraries are only to be used on 32-bit systems and you have to unfortunately re-build ancient code to support a 64-bit architecture, and sometimes you'll have to rewrite sections of it. Hence why handing someone a library file that can't be run would be a waste of time.
Another situation most people will run into is building an executable for a Raspberry Pi to run. It's going to use a different architecture than a typical computer because the hardware is just different (hence the intrigue of having a Raspberry Pi). You'll probably need to build a lot of dependencies from source if you want to use other people's code. You absolutely do not want to compile a large project on your Raspberry Pi. Why? Because it will take like five hours.
A really great thing about your video is that you not only tell us what and why to do, but also conduct those little experiments that deepen our understanding. Well done.
"I just want things y work" oh, a man of culture, I see
18min video on linking a lib... This is whats wrong with C++
Noah Fowler And that's only explaining the basics lol.
Hopefully when c++ 20 adds modules it'll solve this problem ... hopefully
That's because it' actually in-depht and doesn't just say:
"Do this then don"
Had too many times people "explained" it like that but it just ended up in me having no clue what to do because it actually didn't work
And yes, also happened in java multiple times, to be percice: more often even
settle down mr
No the problem is this guy is massively long winded. The intro was way too long, we all know what we're looking for we didn't need the intro talking about his haircut.
Dude ..you are a dude of cpp...love to see your videos..i hated cpp but whenever i feel that way i watch your videos...and my perspective changes..👍
Yes please, make a c++ tutorial video about linking and building the project that ide-independent, using Makefile or Cmake
Since he works for the EA, that would cost you extra :P
Oh God after some years of confusion with C++ Libraries finally a video that worths it. God thanks
In lesson 49, I finally understood the movement of the hand at the end of the video, this is in order to give high five to Cherno through the monitor))
I think CMake videos would be super helpful for people on Linux and Mac. Thanks for the great content!
Apart from the quality of the video, the like is for the statement "you should have everything you need, straightaway, to compile and run"! That's always a big hustle with github projects!
I really enjoyed this tutorial as always. However, I will say that I was pretty confused in the earlier parts of the video. I'd still consider myself kind of a beginner so when GLFW was brought up, I had to go on my own learning-tangent to find out what openGL and GLFW were.
He kept that notification at 1:38 for the trolol xD
1:00 i completely agree, i also hate linking 3rd libraries, but be careful to distribute other source code within your project repo, it might just go against their end user agreement
I got these errors:
· Warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
· Error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _main
I managed to fix them going to *Properties > Linker > Input > Additional Dependencies* and adding
· opengl32.lib
Amazingly detailed yet to the point. Thanks so much man
This one saved me, I was stuck for 2 weeks with the same linking error, thanks Cherno
I programmed in c# the last 4 years but I still got stuck trying to transition to C++.
This video was clear and got me up and running. I wasted hours before I saw this and would have wasted more time if I hadn't seen this video.
I'm going to put the cherno on my tool bar.
Please make a video on building from source! I'd love to see how you approach it! Most people I use use cmake
thank you for existing
Hi Cherno (Bill?). When you get to dynamic libraries in a future video, could you point out the differences between *nix and Windows in this regard. In particular why Linux likes things like theshared.so, theshared.so.1 and perhaps even theshared.so.0.1. Oh and there's ldconfig (?).
Keep up the great videos, you are very good at explaining things.
Yes please. Please go over linux dynamic linking. And maybe also other dynamic linking, such as in your own os.
At this point I am just liking these videos automatically. 31 seconds into the video I remembered that I need to like it... only to realise that I already did
why??? Package Manager is a great idea, you don't have to maintain by yourself the code that you need that others write
Actually the version of visual studio a library was built with does matter because the binary may reference older SDK, linking to older libraries. And you might run into conflict issues when mixing different versions. It may work fine for some libraries like GLFW but it should in general case be discouraged. You'll know you're getting conflict errors in form of somewhat descriptive linker errors.
If the library API is only passing primitive types (int, bool, etc) then there shouldn't be an SDK conflict but if you have a library taking in anything from STD/STL as parameters or return values then the memory layout may have been changed across SDK versions.
Linux have bigger issues with binary compatibility model (linking to linux libraries) due to being updated so frequently, which is why the compile-it-yourself is so common on linux (apart from open source community).
Doesn’t work for glfw anymore, the glfw3.lib file isn’t static only anymore, apparently glfw3_mt.lib is the static build lib now
Watching this in 2020, but I believe this series will be around for many years to come.
What a great video on this topic. Perfect explanation. Helps avoid a lot of pitfalls by understanding the steps. Thank you for sharing.
I can totally understand your pain with hating the current situation with C++ projects' dependencies (had sh*t load of problems trying to get my old projects using Irrlicht engine, for example, compiled years after I wrote that code). But the problem with the approach you are demonstrating (which I used to use too, just years ago) is that if you were about to use a different OS, a different IDE version (imagine trying to open VS 2008 project in VS 2017, which was similar to my case) or even a different IDE (QTCreator, for instance) - you're screwed up. Solution generators (CMake, for instance) can solve this problem, but I hate it as well. But since there is no sane alternative (Bazel / Buck also suck at cross-platform setup), it is waaay better than clicking through the options in your solution for your very specific VisualStudio version.
Why? Why would you dislike his videos? I just fail to comprehend why one would decide to give this incredible guy a dislike. He makes easy to understand, enjoyable and FREE videos that have helped so many people, including me, grow so much as a programmer. Almost all of his videos get not a lot of likes and quite a large amount of dislikes. So, please enlighten me. Why?
Another great video. Now I finally know the difference between static and dynamic libraries, and don't have to scratch my head haha.
Thanks!
First of, your videos are just the best I ever came across on C++ (atleast for me). And, love your long videos, please do more of in-depth. Learning a lot from you. Thanks
I'm definitely interested in building the source code for dependent libraries! Please make that video!
"GLFW binaries have nothing to do with your OS" I don't know if they have to do with my OS, or with my mingw GCC compiler but when I downloaded 32bits GLFW binaries they didn't work. I had to download 64bits binaries in order for it to work on my machine (I have x86_64 windows 7).
Hmm, this is mostly covering using a static library, but not to build your code statically. All sorts of fun issues one can run into there. Was just trying to static compile my program recently with GLEW and had -lGL in my g++ line... lol, of course that didn't work. And so I learned how to get g++ to compile just some dependencies manually (using '-Wl,' before the libraries you can to apply an option to and having that option be -Bstatic and -Bdynamic respectively. Like: g++ myprogram.cpp -o MyProgram -I someFolder -L somelibFolder -L someotherlibFolder -Wl,-Bstatic -lsomestaticlib -Wl,Bdynamic -lsomedynamiclib For those that aren't aware -lGL is provided by the GPU driver... so it's not going to be static or portable.
Some other things that aren't static build friendly: dlopen(), a lot of network libraries, e.g. curl.
I've started liking nuget packages in VS since it now supports native libraries.
I had to watch this video twice and read two articles to help, but now I fully understand thank you Cherno!
Super good. Thanks.
Man, this stuff is still confusing to me and I've had courses in C and C++. Thanks for the deep dives. I love it, and can't wait to start the Game Engine series.
You earned yourself a subscriber from me. Thank-you!
Your videos are very good and well presented. You have changed my view about C++. Thank you.
Oh my god thank you so much. This is exactly what I needed. I'm not used to using visual studio and I just wanted to compile using a library from a third party. Hooray!
i think the best way would be a package manager that copys the libraries in an "libraries" folder in your project
It would be really helpful to see how to do that without visual studio.
>"I hate package managers"
>proceeds to do the work of a package manager entirely by hand
Goddamn it, Cherno
I get it, though, seeing an explanation of how to do it is enlightening, and in C++ it's probably the better way to go
He said why they were bad at the beginning, are you stupid? Most Package managers don't download the package to your project directly, instead they download it elsewhere in your system and link it to your project. You literally missed the entire point.
i gulped before watching this video.
Great explanation, thank you. Despite years of coding in different IDEs and languages, I always got confused how to setup C and C++ projects in Visual Studio.
I used Visual Studio going way back into the early 1990s. No doubt, it is great in that it provides a lot of automation and help to get things done. It is the professional developer's choice for doing all kinds of projects. The thing is for beginners I would say using Eclipse is better because it is simpler and the developer has to understand what is actually going on.
The sound at 1:38 is in the video, it's not you getting an email. You're welcome.
+The Cherno Well... there are reasons to use dynamic, including for security (update the lib in one place and the security patches will be there for everything linking it), and you also get reduced disk space usage overall for the system with this reuse. There are also some double edged swords. A bad update can break lots of code depending on it (experienced this on Arch Linux... bleeding edge indeed). I also sometimes deal with code with very specific dependencies and it is not fun hunting them all down to build a precise environment for ten different apps requiring three different Java versions... that's just gross and my life would be way easier if they statically linked their code. This is why for my own stuff I provide both static and dynamically linked versions (when possible, sometimes I have a dependency that doesn't build statically).
This is a simple problem to solve.
The Operating System is a base runtime that can run your programs. Every system package MUST be a closed circle, not linked to layers above, only below.
The user programs can use the system runtime libraries with dynamic linking, as the developer knows that the system WILL provide those libs in the correct version and location.
Every other lib that a program use that's not on the system runtime MUST be provided with the program, and used statically (even if using dynamic files) to avoid depending on the system repositories.
The system this way have a mission. Provide a SDK to build programs to run on it, and keep runtime compatibility with older versions, in a gap that can keep programs running at least for 10 years without recompiling.
This seems complicated, but actually is the simplest good way (not the simplest way ever) to do things.
Another more complex solution is to separate the system and the programs as two runtimes, and create symbolic links to specific lib versions that the programs need. If a program want x-1.0 then you instal x-1.0, but if another program wants x-1.6 then you install x-1.6, but as your programs will be looking for the same x lib, you create a symlink to the correct version that the program needs. This comes with the cost of keeping every single release of that lib on the repos, but as programs tend to use the latest version, the system can share a lot of code without breaking programs when updating.
@@talkysassis " the developer knows that the system WILL provide those libs in the correct version and location."
@@zvxcvxcz All of this only applies to servers. Windows, OSX ans Android have their stable runtimes.
You know what the runtime have. They don't break it on every new release.
Thanks for doing this video. I was a pretty good C/C++ developer back in the 1990s and very early 2000s. Then I started to do lots of C#, Java, Web Dev, and database development. So after all that time I forgot some of the detailed nuts and bolts of where things go to do C/C++. Oh yeah, I remember now, LOL. Incidentally, after doing all that stuff for many years, I still like C/C++ the best.
For real. C/C++ and Java are pretty much the ancestors of programming today
2:45 Start here.
*My takeaways:*
Static linking vs dynamic linking 5:15
very good
Please do a day in a life of your daily work routine at ea or something similar! That would be amazing. Thanks
Thank you very much. It is very useful.
You are awesome man. Was so confused about everything before watching this.
More linking coming soon in the OpenGL series I'm assuming with GLAD. Good vid, thanks!
THOSE GUITARS AND THE PILLOW
I did everything in the video but am still getting unresolved external symbol error in vs 2019.
What could I be doing wrong.
me too =(
man , you rock
Thank you for video.
Thank you very much for this video, it's help me find the way out, so awesome man!!!
Why did the function call work when you added extern "C"? The include path was not added then how did it find the function?
didn't understand.
If we want to link at run-time, it is good to use .dll.
Else if we want at compile time, it is good to use .lib.
How does dll.lib help?
Thanks so much for this amazing walkthrough!
I noticed in the function declaration that they added void as a parameter. I know that it means that there are no parameters but why do they write it there and don't leave it empty. Is there any reasons why you should do that?
Interesting, I thought leaving the parameter list empty in C defaulted to void. I'll have to research this one because I do it all the time in my C code.
thank you very much
You are my favourite CZcamsr. ❤❤❤❤
Seriously, no, seriously, why do some people dislike these videos? Somebody please explain that to me...
What did you do with the real Cherno??? I can recognize his hair from miles away!
so much thanks
Pretty cool videos that you made. The only issue is the resolution of your screen (very high), however if the internet speed is slow it is difficult to view them in HD.
Love your videos brother!
how can you setup a neural network that takes a api as its source of data
Loved the explanation
Thanks a lot! You are so nice to listen to!
8:02 as an Italian, I'm not complaining
thanks you helped me a lot
Goddamn I love you man :D best tutorial videos there are.
Can u make a video on RTTI in c++ , typeid, kinda stuff
A really useful video. Thank you Cherno!
So good!