Sign in to follow this  
TEUTON

startup function

Recommended Posts

I know there was a way to change your startup function in Turbo C Compiler #pragma startup abc But I want to do that in VC6.0 and DEV C++ Plz tell me that.

Share this post


Link to post
Share on other sites
I had never heard of this pragma before, and seriously, I don't see why you need it.

From what I could gather, this lets you define a function to be called before main()?

Can't you do your processing at the beginning of the main function?

Alternatives I've read about were like:


static int myInitFunc()
{
// do your stuff here
}

static int callInitHere = myInitFunc();

int main()
{
}



or


struct InitStuff
{
InitStuff()
{
// Stuff to be executed before main
}
~InitStuff()
{
// Stuff to be called after main
}
} Init;

int main()
{
}



But once again, why would you need that?

Share this post


Link to post
Share on other sites
Quote:
Original post by xEricx
*** Source Snippet Removed ***

Why are you using static function and static int. Only global function and global int also works fine.

Share this post


Link to post
Share on other sites
No problem.

xEricx's approach is quite a lot easier if all you want to do is to run some code before entering main. But if you want to take complete control over the actual startup function, you'll need to replace the stub. That can be quite a chore - setting up environment/argument variables, exception frames, floating point precision in some cases, and calling constructors and the like. It's a lot of work. Craft a small project - console or gui - with a main or WinMain that does nothing but return 0. Compile it and then disassemble the resulting exe. Actually, to make it easy to locate the main/WinMain function assign an easily recognizable value to an integer inside the function (eg. int x = 0xDEADBEEF; ). Search the disassembly for 0xDEADBEEF and work backwards and fowards from there to determine the start and end points of the main/WinMain function in the disassembly. All of the other code in the disassembly constitutes the stub (for the most part - there might be some data values in the stub that don't end up in the code). If you're gonna replace the stub, it helps to understand what the default stub does. And it also helps to remember that the stub varies from compiler to compiler, so what you learn about the stub used by one compiler doesn't necessarily hold for every compiler.

Share this post


Link to post
Share on other sites
For VC you don't need a custom stub. Actually I'm not sure how a custom stub would be at all relevant.

All you need to do is to pass the /entry switch to the linker and give it the name of your startup function. Please note that doing this sort of thing is fairly serious mojo and you will be bypassing all the usual startup logic that needs to happen to (e.g.) get the C/C++ runtime libraries initialized properly and get the global state setup. You definitely shouldn't do that sort of thing unless you know exactly what you're getting into, have a very specific reason, and can't do it any other way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
For VC you don't need a custom stub. Actually I'm not sure how a custom stub would be at all relevant.

All you need to do is to pass the /entry switch to the linker and give it the name of your startup function. Please note that doing this sort of thing is fairly serious mojo and you will be bypassing all the usual startup logic that needs to happen to (e.g.) get the C/C++ runtime libraries initialized properly and get the global state setup. You definitely shouldn't do that sort of thing unless you know exactly what you're getting into, have a very specific reason, and can't do it any other way.


The "serious mojo" that you describe amounts to the same thing as a custom stub.

Share this post


Link to post
Share on other sites
We must be talking about different things with the word "stub". By stub I mean the DOS stub that every Win32 executable has embedded in it who's original purpose was to tell a DOS user that this app isn't actually a DOS app and that you need to be running Windows. This code doesn't even get touched by modern versions of Windows and has no effect on application startup.

It sounds like you are using "stub" to mean the C/C++ runtime initialization code. I've never heard that referred to as a stub before.

Share this post


Link to post
Share on other sites
I'm using the term stub more broadly to refer to the object file(s) that contains the initialization code. These files usually have 'crt0' in their name. A more accurate name for what I refered to as the stub would be "Default Libraries" - the object files that contain WinMainCRTStartup/mainCRTStartup.

As you point out, the DOS stub is a useless vestige that has no effect on application startup. I would have refered to that as the DOS stub.

And as you point out, using a linker switch would be an easier approach than replacing the default libraries. The links I dropped, use the linker switch.

Share this post


Link to post
Share on other sites
By reading the first link...I did follwing changes to VC's project setting...in linker settings

/NODEFAULTLIB
/INCREMENTAL:NO
/ENTRY:function


#include <stdio.h>
void function(void)
{
printf("Hello World");
}
/*int main(void)
{
return 0;
}*/



But now I get errors
--------------------Configuration: Test - Win32 Debug--------------------
Linking...
Test.obj : error LNK2001: unresolved external symbol _printf
Test.obj : error LNK2001: unresolved external symbol __chkesp
Debug/Test.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

Test.exe - 3 error(s), 0 warning(s)

I am not able to use printf now...how should I output now????

Share this post


Link to post
Share on other sites
Specifying /NODEFAULTLIB tells the linker not to link with the default libraries - which includes printf() and the debug stack checking function (__chkesp) among other things.

Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
Quote:
Original post by JohnBSmall
No. You don't want to do that.

???

Every time you compile something in VC6, God kills a kitten.

Please, think of the kittens. (download VC2005)

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBSmall
Quote:
Original post by TEUTON
But I want to do that in VC6.0 and DEV C++

No. You don't want to do that.

John B


If even you do, why go with something compiler dependent when you can just define something (not that I would go defining stuff, I like to see "main").

#include <stdlib.h>

#define super_cool_startup_func main

int super_cool_startup_func(int, char*[]) {
printf("This is my super-cool startup function!!!\n");
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
Every time you compile something in VC6, God kills a kitten.

Please, think of the kittens. (download VC2005)


:):)

I am happy with VC6.0

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Remove /NODEFAULTLIB ?


When I do that I get

--------------------Configuration: Test - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Test.exe - 2 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
Just save yourself the effort and put

#define MyNewMainFunctionLOL main

at the top of your file or something. Seriously. There's no good reason to be bothering with stuff like this, unless you really know what you're doing. And people who generally know what they're doing don't ask how to do things they already know how to do. Or something.

Share this post


Link to post
Share on other sites
Why do you need to call something before main, exactly?
Wouldn't this have the same effect?


void foo()
{
}

int main()
{
foo()

//Other stuff

return 0;
}



It seems like you're trying to do very complicated things without knowing or being able to explain why you need to do them in the first place.

I'll also hop on the 'Use VC2005' bandwagon. VC6 is about 10 years old and is pre-standards.

Share this post


Link to post
Share on other sites
You could try putting in a dummy main() function. If you tell the linker to use your own entry point, then it should ignore it - although I don't know.

You'll ahve to read those links LessBread linked - that explains all the stuff you need to do if you change the entry point.

Why are you doing this anyway? What's wrong with Mushu's suggestion?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this