last day (15 days later) » 

7:48 AM
0
Q: How to create a static library that is statically linked against c/c++ standard libraries?

Kim Jong UnI want to create a trivial static library my_math.lib. Its header and source files are given as follows. // my_math.h #ifndef MY_MATH_H #define MY_MATH_H double reciprocal(double d); #endif // my_math.cpp #include "my_math.h" #include <iostream> double reciprocal(double d) { std::cout << __...

 
@Nina: In which step (among 4 steps given in my question) can I use -static option? I don't get it.
@Nina: Please be clearer, what do you mean by compiling my_math.lib? Step 2?
@Nina: For academic and learning purposes as I already wrote above.
@Nina: I know g++ -c my_math.cpp can produce my_math.o without passing through .ii and .s. Now I am trying to use -static.
@Nina: I think -static is only for producing exe. I got an error:
g++ my_math.s -o my_math.o -static-libstdc++ C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../‌​../x86_64-w64-mingw3‌​2/lib/../lib/libming‌​w32.a(lib64_libmingw‌​32_a-crt0_c.o):crt0_‌​c.c:(.text.startup+0‌​x2e): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status
I will be back in about 1 hour.
 
Old link but still good Static, Shared Dynamic and Loadable Linux Libraries (seriously, can do dial back the ICBM tests a bit?...)
 
@DavidC.Rankin: Thank you very much for the link. Hopefully there is the answer to my question there.
 
There is. It shows you how to create and use both types of libraries, static and dynamic. (remember, a static library is basically an archive of the functions, the command ar is used to interact with them in a number of ways)
 
@DavidC.Rankin: More precisely, we can create our own static libraries that are statically linked against c/c++ standard libraries such that our own static libraries become self-contained thereby any executable apps that are statically linked against our own static libraries no longer need c/c++ runtime libraries, right?
 
7:48 AM
Strike that, Yes, the executable loading will be provided by the OS, so on windows the windows executable is loaded or on Linux the ELF is loaded. You are creating a library (static or dynamic) that contain the symbols (and functions attached to those symbol) so when your code executes the machine code for them is there.
 
@DavidC.Rankin: I am getting more confused. In my understanding, if executable applications are statically linked against c/c++ standard libraries, they will no longer need c/c++ runtime because static linking makes the apps self-contained but their file size become bigger. Sorry, am I wrong here?
@DavidC.Rankin: OK. I am reading the article in your link. If no answer there, I will dial back the ICBM tests and commit suicide.
 
Think of it this way. You have your basic "Hello World" program. You compile and link it. It is a self-contained executable for the OS on which you built it. It contains all the C run time code in the executable format. Now you create another source file that has a function that outputs "Goodbye World" and compile it into a library. You include a header for that in your Hello program. You then have to link Hello against that library. It doesn't matter if it is static or dynamic. It is what has the Goodbye code. Either form of library can provide that code.
I think where you are getting hung up is the library is something You Will Provide. So it doesn't matter what type of library you use. You will still have to provide a library (unless you compile and link all the functions with your original program). Static libraries are not used much because they are huge compared to dynamic shared object libraries because they include all the overhead needed for the functionality they provide. In either case, your library isn't something that will already be on the target machine.
 
@DavidC.Rankin: If we can compile Goodbye World into a static library that is statically linked against c/c++ libraries, I expect the library size become bigger than if I compile Goodbye World into a static library that is dynamically linked against c/c++ libraries. This is what I want to observe for academic purposes.
 
Yes, that is 100% the correct way to think about.
 
@DavidC.Rankin: Thank you for your time!. I am reading... :-)
 
7:48 AM
It has been ages since I messed with static libraries (KDE3 timeframe). Several good articles should be all you need. I would search "C++ static dynamic library difference" and you can probably find all you need in the first page of links :) Good luck with your project!
 
@DavidC.Rankin : Nothing. There are no steps (in the article you gave) to create a user-defined static library that is linked against c/c++ standard libraries.
 
Ohhh... We have been talking about different things. If you just have a separate source file with functions and you are calling that a "library", there is nothing special required. You can simply compile and link that at the same time, e.g. gcc -o my_program hello.c goodbye.c done. Or you can compile goodbye.c to object with gcc -c -o goodbye.o goodbye.c and then link that will hello as gcc -o my_prog hello.c goodbye.o
 
@DavidC.Rankin: But it is not the question.
If this question does not make sense, I will delete it in a couple of days.
 
"is it possible to create a static library my_math.lib that statically links against c/c++ libraries?" Now I'm confused. If you don't want to create a -static library from my_math.o and you don't want to create a shared-object library. Are you wanting to dynamically load the static library at runtime? If so, that's a no-go, and the answer is here dynamically loading static library?
 
@DavidC.Rankin: I WANT to create my own static library that is statically linked against c/c++ standard libraries. The final output is a .lib file that contains both my own code as well as c/c++ standard libraries. Is it not possible?
@DavidC.Rankin: In other words, is it possible to merge c/c++ standard libraries and my own libraries into a single static library?
 
7:49 AM
Understand when you create an object file (that can be a part of a static lib) it is already compiled against c/c++ standard libraries. It is the linker step that determines the static linkage. Yes, you can create your library and link directly against the C runtime and libc. That is discussed under the OPTIONS section of man 1 ld.
The result would be linked directly against the c/c++ standard libraries. But that is more in creating a final executable that creating a library.
 
So it is not possible?
without example, confused.
the manual is huge, alot of information that might not be relevant
 
8:18 AM
I think it boils down to what you end up with after you "link". If you want to "statically linked against c/c++ standard libraries" -- then you end up with the output of the linker, not compiler. So what you have is in the form of the final executable, not library. Leave the question open for a while and see if you get so other deep magic I'm simply unaware of, but I don't think you can do what you are wanting to do.
I'm interested as well in if there is any such animal...
 
 
1 hour later…
9:21 AM
@DavidC.Rankin: thank you very much for your answer. Hmm. I think you are correct!
Static linking so it must be about linker that produces executable binaries.
 
 
2 hours later…
11:32 AM
@DavidC.Rankin: The problem has been solved by PeterT.
 

last day (15 days later) »