Solved

Getting rid of C Run-Time Library. How?

Posted on 1998-03-25
11
778 Views
Last Modified: 2011-09-20
Hi:

I am developing a DLL for system hook, so I'm trying to minimize its size. I am using just C++/Win32, no MFC (IDE is Visual C++ 5.0).

Is it possible to link the DLL so that the C Run-Time Library code will not be loaded when my DLL gets loaded?

I am aware of linking with DLL form of CRTL, but in this case the whole CRTL is also loaded which I want to prevent.

Is it possible to get rid of CRTL code?

Regards,
Asker.
0
Comment
Question by:asker
  • 6
  • 4
11 Comments
 
LVL 1

Expert Comment

by:focht
ID: 1183780
Get rid of the CRT:
- In the linker options there is an "ignore libraries" edit box. Enter Libcmt.lib into it, and build. You get several unresolved externals. This list is everything that you are using from the CRT. Look for things that you think may be pulling in the startup code and remove them if you can.

Another method to determine if you need the startup code:
- Don't ignore Libcmt.lib, but turn on the verbose flag for the linker. From this, you can see what is triggering CRT startup code to get pulled in.

You can use some C-Runtime functions without requiring the CRT startup code. Examples include the mem* functions. Other functions require the CRT startup code. CRT string comparisons for example require the startup code as the CRT initializes some tables used for comparing. Global objects that have constructors also require the startup code. In Visual C++ 5.0, statically linking the startup code adds about 25K to your image(In Visual C++ 4.2 it is about 20K).
If you use functions or code in your project that require the use of the C-Runtime library startup code, you may experience LNK2001 - unresolved external errors when you try to build the release version of your project.
 
0
 

Author Comment

by:asker
ID: 1183781
To Focht:

Here are the references I've got:

error LNK2001: unresolved external symbol __chkstk
error LNK2001: unresolved external symbol _atexit
error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
error LNK2001: unresolved external symbol __fltused
error LNK2001: unresolved external symbol __ftol
error LNK2001: unresolved external symbol ___CxxFrameHandler
error LNK2001: unresolved external symbol __except_list
error LNK2001: unresolved external symbol __DllMainCRTStartup@12

How can this help me to get rid of CRTL? I am even ready to rewrite my DLL on a plain C (instead C++).

Regards,
Asker.
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1183782
In order to get rid of the runtime library, you have to stop using it. That means you cannot make any calls to the runtime library, such as delete. You must also turn off exception handling. Take a look at an article by Matt Pietrek on www.microsoft.com/msj and search for tinylib for a runtime library replacement.


0
 

Author Comment

by:asker
ID: 1183783
To Thui:

I did not use any of those unresolved externals in my DLL (there are no explicit calls to delete also). The comment for DllMainCRTStartup in CRT source code tells that it does (among other things) classes' constructor calling. Does it mean that it is impossible to get rid of CRTL in C++ completely?
Would it be better in C? Or it is enough to stop using classes? Or the problem is in something else?

I could not find the article you mentioned on www.microsoft.com/msj.

Regards,
Asker.

0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1183784
Yes, you cannot use C++ without the runtime library. You will have to use C for that. Then on top of that, you cannot make any calls into the runtime library too.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:asker
ID: 1183785
To Thui:

Thank you for the answer. I'm going to convert all my sources to C.

:-(

Regards,
Asker.
0
 

Author Comment

by:asker
ID: 1183786
To Thui:

After converting my DLL to C, I still have these:

error LNK2001: unresolved external symbol __chkstk
error LNK2001: unresolved external symbol __fltused
error LNK2001: unresolved external symbol __ftol

I found out chkstk.obj in LIB directory, linked it and __chkstk disappeared.
But what should I do with fltused and ftol?

Regards,
Asker.

0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1183787
Are you using floating points?

0
 

Author Comment

by:asker
ID: 1183788
To Thui:

Yes, I am using some calculations with float values, than casting the result to int. Does that mean I have to link with CRTL? Or there is some magic file only_fp_routines.obj (like chkstk.obj)?

Regards,
Asker.

0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1183789
Try using doubles instead of floats and see what happens.


0
 

Author Comment

by:asker
ID: 1183790
To Thui:

I have tried - nothing changed:-( As far as I understand, _ftol is the routine which converts a float/double result to an integer value (ftol stands for float-to-long?).

_fltused (stands for float-used?) is a magic constant, defined in CRTL, _ftol I was unable to locate at all.

I do need to perform some simple float calculations - taking a fraction of an integer value, while the value of the fraction is calculated as a ratio between the two other integer values, looks like:

int a, b= 10, c = 12, d = 20;

a = (int)(float)(( ((float)b / (float)c) * (float)d));

Any ideas? Maybe some home-made FP library exists? It shouldn't be fast and very exact.

Regards,
Asker.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now