main function: argc is negative number

Thema 21 Reputation points
2020-10-14T13:47:10.78+00:00

Hi fellow programmers;

I am writing a cross platform linker using the Microsoft SDK
but the argc argument is negative. I have written a batch
script to build the test program which is called main_Test1.bat.

The following is the batch script:

cl.exe /c /TC /Gz /MD /w main_Test1.c

CD ..\objs

link.exe /SUBSYSTEM:CONSOLE /ENTRY:test_main /OUT:..\tests\main_Test1.exe ..\tests\main_Test1.obj ^
factory.obj io.obj main.obj util.obj vlid.obj kernel32.lib msvcrt.lib

CD ..\tests

PAUSE

The following is the code:

int test_main(int argc, char *argv[], char *env[]){

 //Variable declarations.
 int ret;

 //Argument checks.

 //Initializations.

 //Main logic.
 printf("\nargc = %d\n", argc);

}

When I build and run the test program, I get a negative number
for argc.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
{count} votes

Accepted answer
  1. RLWA32 45,236 Reputation points
    2020-10-14T14:31:46.52+00:00

    Give this C example a try -- set the entry point to RawEntryPoint. Make sure to link with all the defaults. Its easiest to test this by building it in the VS IDE.

        #include <stdio.h>
    
        int mainCRTStartup();
    
        int __stdcall RawEntryPoint(void)
        {
            return mainCRTStartup();
        }
    
        int main(int argc, char *argv[], char *env[]) {
    
            //Variable declarations.
            int ret;
    
            //Argument checks.
    
            //Initializations.
    
            //Main logic.
            printf("\nargc = %d\n", argc);
    
        }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.