# startup function

## Recommended Posts

TEUTON    100
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 on other sites
xEricx    572
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?

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 on other sites
TEUTON    100
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 on other sites
xEricx    572
By declaring them static it makes them only available from within the file where main() is.

##### Share on other sites
TEUTON    100
Quote:
 Original post by LessBreadYou have to build your own stub file. Here are a few links to get you started.Creating Small Win32 Executables - Fast BuildsReduce EXE and DLL Size with LIBCTINY.LIBTechniques for reducing Executable sizeCreating Self-Installing Packages

##### 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 on other sites
Anon Mike    1098
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 on other sites
Quote:
 Original post by Anon MikeFor 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 on other sites
Anon Mike    1098
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 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 on other sites
TEUTON    100

/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--------------------
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

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

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

##### Share on other sites
Evil Steve    2017
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 on other sites
TEUTON    100
How should I get the output then???

##### Share on other sites
Evil Steve    2017
Remove /NODEFAULTLIB ?

##### Share on other sites
JohnBSmall    881
Quote:
 Original post by TEUTONBut I want to do that in VC6.0 and DEV C++

No. You don't want to do that.

John B

##### Share on other sites
TEUTON    100
Quote:
 Original post by JohnBSmallNo. You don't want to do that.

???

##### Share on other sites
Mushu    1396
Quote:
Original post by TEUTON
Quote:
 Original post by JohnBSmallNo. You don't want to do that.

???

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

##### Share on other sites
nullsquared    126
Quote:
Original post by JohnBSmall
Quote:
 Original post by TEUTONBut 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 mainint super_cool_startup_func(int, char*[]) {    printf("This is my super-cool startup function!!!\n");}

##### Share on other sites
TEUTON    100
Quote:
 Original post by MushuEvery time you compile something in VC6, God kills a kitten.Please, think of the kittens. (download VC2005)

:):)

I am happy with VC6.0

##### Share on other sites
TEUTON    100
Quote:
 Original post by Evil SteveRemove /NODEFAULTLIB ?

When I do that I get

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

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

##### Share on other sites
Mushu    1396
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 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 on other sites
Evil Steve    2017
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 on other sites
xEricx    572
Can you explain us what you're trying to do and maybe we'll be able to give you more explanations on how to achieve it?