Using Libraries in C++ (Static Linking)
Вставка
- Опубліковано 27 вер 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
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
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.
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
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
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
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
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?
Great stuff as always , and yes linking libs from source code video would be a nice add to this series
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
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
Thank you for going in depth. I watch all your videos to the end.
i too they are simple and in depth
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.
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?
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
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.
"I just want things y work" oh, a man of culture, I see
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.
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
Oh God after some years of confusion with C++ Libraries finally a video that worths it. God thanks
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.
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
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
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
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.
I'm definitely interested in building the source code for dependent libraries! Please make that video!
What a great video on this topic. Perfect explanation. Helps avoid a lot of pitfalls by understanding the steps. Thank you for sharing.
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
I had to watch this video twice and read two articles to help, but now I fully understand thank you Cherno!
More linking coming soon in the OpenGL series I'm assuming with GLAD. Good vid, thanks!
thank you for existing
Another great video. Now I finally know the difference between static and dynamic libraries, and don't have to scratch my head haha.
Thanks!
blood love your work you are a savior
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.
You earned yourself a subscriber from me. Thank-you!
+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.
why??? Package Manager is a great idea, you don't have to maintain by yourself the code that you need that others write
You are awesome man. Was so confused about everything before watching this.
It would be really helpful to see how to do that without visual studio.
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
2:45 Start here.
*My takeaways:*
Static linking vs dynamic linking 5:15
8:02 as an Italian, I'm not complaining
thank you very much
I've started liking nuget packages in VS since it now supports native libraries.
so much thanks
Thank you for video.
very good
>"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.
What did you do with the real Cherno??? I can recognize his hair from miles away!
oh boi, helped me a lot, coding rust hack here. Using c++ and c#
I've had the link2019 error, solved that, but with Xenos injector i got error, cause its an external .lib so i going try add this to project, thx a lot for this video explanation about this subject.
Thanx
Can u make a video on RTTI in c++ , typeid, kinda stuff
thanks you helped me a lot
The sound at 1:38 is in the video, it's not you getting an email. You're welcome.
do you know vcpkg? package manager made by microsoft for c++ . I think you should introduce it. I am using and it is very useful.
Goddamn I love you man :D best tutorial videos there are.
How to include sub-directories in Visual Studio?
I have to include many header files, which are in different sub-directories. Is there a way in Visual Studio (I am using 2013 edition) to set one include path that Visual Studio will search also the sub-directories for header files?
My Project is in C/C++.
Add the "base folder" to the project (project properties -> Configuration Properties -> C/C++ -> Additional Include Directories, "additional include directories")
I have tried the above option but it is not possible for me to add each and every directory followed by a semicolon.
I have total 60 + different C C++ sub-directories
Nice video, keep going! What is the brand of your watch???
You are my favourite UA-camr. ❤❤❤❤
THOSE GUITARS AND THE PILLOW
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?
man , you rock
very useful sir
i think the best way would be a package manager that copys the libraries in an "libraries" folder in your project
Very Nice thanks
how can you setup a neural network that takes a api as its source of data
How and can i dynamically get a class from, with LoadLibrary loaded, function?
A video documenting how to do this on other platforms would be great, say something like cmake if you were to build from source
very nice explanation i understood everything
where did you learn these things?
Thanks that's really help me understand c++
10:15 is where he shows how to link it
Seriously, no, seriously, why do some people dislike these videos? Somebody please explain that to me...
Please, A tutorial on Visual Studio!
ps:
great videos, keep them coming. I just get very lost when you get into the project config page.
So good!
very clear! thanks
Would you please do video about JSON parsing in c++
Hey Cherno, thank you for the great content.
Are you planning on making an episode on Boost/STL ?
Definitely want to hear about building libraries to link myself. I woul also like to hear about how the header files and linker work in the context of a binary. In your video on the linker, it worked very much hand-in-hand with the compiler, so if the compile step has already been completed for the binary, how does the linker know how to hook up both your binary and the already-compiled binary that you don't have the source for? Is there something in the header files that tells the linker what bits in the compiled binary correspond to which functions you would be calling out of it?
With symbols :P
Good haircut man!
New haircut !!!
How hard you stroke that keyboard ??
Very very good explanation!!!!
But how do I do it with GCC or just not with VS?
Oh dude u make really good video
Well done! Thank you!
I took a game engine programming class at my University and by far the most important skill I learned was how to build all of our dependencies from scratch. You should definitely make a video on that.
I also don't like package managers too much, I don't know why but many people seem to love them to no end.
I keep getting this build error: "library machine type 'x86' conflicts with target machine type 'x64'" for quickfix.lib
I understand the error but does this mean I can never create a x64 application if even 1 dependency is x86?
Yes, because that dependency will only work for x86.
1:20 I thought this was the typescript series. That explains why nothing has been working properly yet
Really enjoying your videos. One question I have is when would I use a .def to specify exported functions? Are .def files still needed today?
Followed all steps multiple times, verified multiple times, I did exactly the same, but I still get that same error...
When using libraries why do we have to build it to generate lib files and include header files? If we have the source code for the library can we not put the cpp files and header files in our repository and include it directly?
I spent two hours figuring out why I couldn't run the tutorial of opengl on Ubuntu. You saved my pc from getting shmucked
*An hour before my programming assignment is due*
Me: 1:28
i love you thanks for clearing everything up for me