Solved

Getting rid of C Run-Time Library. How?

Posted on 1998-03-25
11
777 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:asker
Comment Utility
To Thui:

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

:-(

Regards,
Asker.
0
 

Author Comment

by:asker
Comment Utility
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
Comment Utility
Are you using floating points?

0
 

Author Comment

by:asker
Comment Utility
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
Comment Utility
Try using doubles instead of floats and see what happens.


0
 

Author Comment

by:asker
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

771 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

13 Experts available now in Live!

Get 1:1 Help Now