Hacker News new | past | comments | ask | show | jobs | submit login

  int main(){ puts("Hello World"); }
> This doesn't work in Windows.

Sure it does. Windows has console programs. Though I'd argue that your use of non-standard main is not something I'd do, but IIRC even that works on Windows, at least it did with older compilers, haven't done much windows C programming with the newer ones.




What you point out is an _abstraction_ that compilers include as part of their kits.

You claim to be a low-level programmer who understands C. The truth is right in front of you. Decompile those programs, look at their symbol tables. Notice, every Win32 program starts at WinMain, called with the arguments that I listed above.

Come back when you've done this simple exercise. Realize, WinMain is the _true_ starting point of "C Programs" in Windows. The rest are compiler abstractions.

http://msdn.microsoft.com/en-us/library/windows/desktop/ff38... >>> How does the compiler know to invoke wWinMain instead of the standard main function? What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain.


The true starting point for PE executables is AddressOfEntryPoint in the PE header, with a few parameters pushed on the stack. WinMain is a compiler abstraction; it is not looked up by name.

You can't code in a high-level language like C without working with abstractions. Whether you look at the main() level or the WinMain() level, there will still be library initialization hooks running before your end-user code gets to run.


I'm gonna upvote your comment as you are one of the few people in YCombinator who seems to know what they're talking about. Good job catching that, and you're right. The PE Header contains the entry point. (And the MZ Header is potentially a 2nd entry point left in for DOS Compatibility purposes)

The rest of the people here talking as if C is some sort of ultra-portable magic language need to learn about the low level details that differ between OSes.

My primary point remains however, the easiest and most straightforward way to interface with Windows libraries is through C++ and C#. Even C itself is a high-level language built on top of abstractions built by compilers and linkers.


Show me where in the ANSI standard it says this is illegal. Implementation details do not matter. The Microsoft C compiler will compile a ANSI C89-conforming program into a runnable executable on Windows. The rest is undefined.

And while we're on implementation details, please note that most UNIX platforms do not start at main either. Most platforms include some soft of crt0 that must be linked which contains the real entry point. The dynamic linker will also run code prior to main.

But the C standard only defines what the environment looks like when execution begins and makes no statement on what might run before or how the program got into memory in the first place.


Come to think of it, I don't think I've used any platform where main() is the real entry point to the executable. On Linux the real entry point is _start, all of the bare-metal embedded stuff I've touched does a whole bunch of hardware setup in crt0 before it calls main(), etc...


main() is the entry point function in newer OS X versions. Why ship crt0 in everything when you can put it in dyld?


crt0 would still get linked into static binaries in OSX, I presume.



> Realize, WinMain is the _true_ starting point of "C Programs" in Windows

Actually I do know that, but you simply said "int main() blah blah blah" doesn't "work" (to quote you there). If you're going to require me to be precise, maybe you should look in the mirror first.


Asm output from a c program that prints hello world to the screen. You can figure out the other sections I'm sure.

No win main or other similar concepts here and built using vs2012.

Microsoft (R) COFF/PE Dumper Version 11.00.61030.0 Copyright (C) Microsoft Corporation. All rights reserved.

Dump of file consoleapplication6.exe

File Type: EXECUTABLE IMAGE

_main: 00401000: 68 48 20 40 00 push offset ??_C@_0M@KIBDPGDE@Hello?5world?$AA@ 00401005: FF 15 00 20 40 00 call dword ptr [__imp__puts] 0040100B: 83 C4 04 add esp,4 0040100E: 33 C0 xor eax,eax 00401010: C3 ret

  Summary

        1000 .rdata
        1000 .reloc
        1000 .rsrc
        1000 .text


I've tried to do programs with /nostdlib before, how did you implement the startup and shutdown functions that are required?


Sorry about that I didn't see your reply! Because I have to use phone it's challenging to type out all I did. While I achieved it, I wouldn't say it's sensible or easy!

Anyway, you have to turn off c++, c++ exceptions, security checks in compiler options. In linker set the entry point to main and turn off error reporting. From there it's finding the right lib which is dependent on arch, etc.

To be honest it's not worth doing. I only knew of it because I had to patch a vs2010 exe to run on windows 2000.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: