hand-linking to stdlib.h in mingw

Started by
20 comments, last by Aardvajk 10 years, 2 months ago

I would like to not include whole "stdlib.h" or "math.h" or "stdio.h"

in my modules but write only used declarations with my own hand for

example

extern int rand(void);

etc

but when i comment //#include "stdio.h" mingw linker complains that it do

not see the symbols (though i wrote the declaration)

I suspect that it some cheat that maybe automaticaly links to stdlib binary when i am usling include and do not links to it when i do not use include, so i should

maybe provide it with -lsomething - but i do not know what to write in place

of something

am I right ? can i use symbols with including all the headers, what to -l link

in linker to make it work?

tnx for help

(im compiling in c++ mode othervise c code if this is important)

Advertisement

Since you are compiling it as C++, try declaring the C functions as actual C functions, like this:


extern "C" {
int rand();
// whatever else
}

Otherwise the C++ name mangling will prevent the linker from finding the correct function.

I guess that you are doing this to reduce compilation times? If so, mingw supports precompiled headers, which are probably a better way to deal with this, than rewriting the standard headers.

Since you are compiling it as C++, try declaring the C functions as actual C functions, like this:


extern "C" {
int rand();
// whatever else
}

Otherwise the C++ name mangling will prevent the linker from finding the correct function.

I guess that you are doing this to reduce compilation times? If so, mingw supports precompiled headers, which are probably a better way to deal with this, than rewriting the standard headers.

grrreat, much tnx, it worx (overlooked it)

besides though - a binary for stdlib.h is not provided in command line like other normal libs, so i wonder what it is given here implicitely -

does maybe someone know? where lies the binary?

I am doing this mainly for clarity - i wouldlike to give this with my own

hands, I just prever to give simple

extern "C" int rand (void);
like here instead of /#include <stdlib.h>
(not sure though if with windows.h declaration it will go that easy,
though i would like to cheat its types (like HWND with ints etc) get some declarations for 20 functions i use and also throw it all other away) ;/
tnx for help

It is a part of the standard library that is, most likely, implicitly linked to your program. It contains more than just some random functions (pun intended, I quess) for you to use, such as the necessary runtime environment, so not linking it is not really an option.

It is a part of the standard library that is, most likely, implicitly linked to your program. It contains more than just some random functions (pun intended, I quess) for you to use, such as the necessary runtime environment, so not linking it is not really an option.

well, interesting remark, but even if this is 100% neccessary (i am not sure) there would be an option tolink to other runtime maybe

except that i still do not know where lies the binary :U

What you are doing is not very sensible, nor will it do what you want. If you want to avoid linking to the standard library, give MinGW the link-option -nostdlib. Note that since you use the standard library your programs will not work afterwards.

MinGW will link to the system's MSVCRT, and on demand to the C or C++ standard libraries (statically or dynamically, depending on what MinGW distro you use) without you having to do anything. Tampering with this will result in programs that do not work, but not including a header will not make any difference here.

Including <stdlib.h> is none less clear than declaring functions by hand (on the contrary, it is the correct thing that people expect to see). You further risk omitting some attributes on a couple of functions without noticing, or getting an #ifdef wrong by accident, which may cause your program to behave incorrectly, or may result in non-portable programs, or may not give the compiler necessary information needed to generate optimal code and avoid warnings.

It is easy to get a typedef wrong or to forget an attribute on some unimportant function. The result will be, in the best case, a compile error. Or, in the worst case, a program that doesn't work properly (or maybe a program that works properly in a 32bit build, and doesn't work in 64bits), and nobody can tell why.

Yes, you can link to other libc implementations. For desktop Linux, people usually use glibc. You can use uClibc or some other libc, but most are not going to be as full-featured since they're often aimed at embedded development. You can also just change the source to glibc and compile it yourself. There's a rand.c file right there in the stdlib folder of the glibc source distributions.

For Windows, you'd probably be linking against msvcrt instead of glibc. I think Cygwin uses its own thing instead of msvcrt, but mingw definitely uses msvcrt. Your options are just going to be way more limited on Windows, so it's probably easier to just run Linux while you're learning whatever you're hoping to learn from this.

If for some reason you want to compile with no standard library at all, in gcc you can use -nostdlib option. You might need to also use -exclude-libs or some other stuff. I just don't know what all you'd have to do.


What you are doing is not very sensible, nor will it do what you want. If you want to avoid linking to the standard library, give MinGW the link-option -nostdlib. Note that since you use the standard library your programs will not work afterwards.

MinGW will link to the system's MSVCRT, and on demand to the C or C++ standard libraries (statically or dynamically, depending on what MinGW distro you use) without you having to do anything. Tampering with this will result in programs that do not work, but not including a header will not make any difference here.

Including <stdlib.h> is none less clear than declaring functions by hand (on the contrary, it is the correct thing that people expect to see). You further risk omitting some attributes on a couple of functions without noticing, or getting an #ifdef wrong by accident, which may cause your program to behave incorrectly, or may result in non-portable programs, or may not give the compiler necessary information needed to generate optimal code and avoid warnings.

It is easy to get a typedef wrong or to forget an attribute on some unimportant function. The result will be, in the best case, a compile error. Or, in the worst case, a program that doesn't work properly (or maybe a program that works properly in a 32bit build, and doesn't work in 64bits), and nobody can tell why.

I know it is 'hackish' or what to call this [but would like to try it, Im not saying i will be using this all the time (I was doing already 'worse' things ;/0]

As to this msvcrt.dll i heard that this is not system (at least win xp i am using) dll - this must be installed - You sure mingw links against that, where is the import.lib for that? does maybe someone kbow more about this implicit linked libraries - this is important to know imo

Yes, you can link to other libc implementations. For desktop Linux, people usually use glibc. You can use uClibc or some other libc, but most are not going to be as full-featured since they're often aimed at embedded development. You can also just change the source to glibc and compile it yourself. There's a rand.c file right there in the stdlib folder of the glibc source distributions.

For Windows, you'd probably be linking against msvcrt instead of glibc. I think Cygwin uses its own thing instead of msvcrt, but mingw definitely uses msvcrt. Your options are just going to be way more limited on Windows, so it's probably easier to just run Linux while you're learning whatever you're hoping to learn from this.

If for some reason you want to compile with no standard library at all, in gcc you can use -nostdlib option. You might need to also use -exclude-libs or some other stuff. I just don't know what all you'd have to do.

well i just want to throw away whole includes (it is about c standard library headers and also windows.h) and write declarations with my own hand only

also i would need to know exactly where are all the binaries mingw is

linking my binaries against? Dont you know where lies import library used for linking with this msvcrt.dll ? (I asume this is dynamic lib only linked against? not static one?

Is this containing only some part of standard c library things or all this c library

is in it?

tnx for hints

http://msdn.microsoft.com/en-us/library/abx4dbyh.aspx explains the different msvcrt libraries. It is all of the c runtime library (with exceptions spelled out in that link). That's what the crt stands for: C RunTime.

This topic is closed to new replies.

Advertisement