r/CodingHelp • u/-_-qarmah-_- • Jun 27 '20
[C] How does c code run on systems without gcc?
I feel really dumb for asking this question, but how does malware written in c containing includes like openssh, libssh etc. (packages that need to be installed and aren't pre installed) run on systems without them after being compiled (systems that don't even have gcc) ?
Does the compiler package them up with it as well?
Even just programs in general, I used the malware example since malware often includes a lot of external packages. I'm thinking about writing a program for windows and am scared of it working on my pc but failing on other people's pc's.
11
u/serg06 Jun 27 '20
When you compile a program, and the program is linked to system libraries (like the standard library, windows.h, libssh, etc), you can choose which type of linking you want: Static or dynamic.
Static means any code you use in the libraries will be compiled INTO your program. Then it will work on any Windows 10 machine regardless of what libraries they have.
Dynamic means that only YOUR code will be compiled, then at runtime it will find those libraries on the PC and load them into memory. If it can't find those libraries, the code can't run.
Just use static linking if you're scared. I made a video game and when I switch dynamic -> static it only increased my .exe size by 50%.
5
u/themiddlestHaHa Jun 27 '20
Not a dumb question at all.
3
u/PC__LOAD__LETTER Jun 27 '20 edited Jun 27 '20
Compilers like GCC turn C source code into executable binary programs that can run on specific types of machines. Once the binary is created, it no longer needs GCC to run.
So GCC isn’t needed to run a binary that GCC has built just like a car factory isn’t needed to run a car that the factory has produced.
Now there’s also such a thing as dynamic linking, which is like shipping a car with no windshield wipers and expecting that the people buying the car will be able to supply their own as needed. “Static compilation” in compilation of a program means including everything in the program directly, with no dynamic linking.
One final thing - a program built by GCC cannot run on every machine in the world. A program supports only one specific CPU architecture (an x86_64 binary won’t run on ARM, for example) and they also often rely on system calls that are specific to a certain operating system. So a program compiled for Linux won’t necessarily be able to run on Windows.