-2

I get the following error Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' when I compile my code using Win32 Console Application. I tried fixing it my going into Projects -> Properties -> General -> Linker -> Enable Incremental Linking and I changed it from "Yes" to No (/INCREMENTAL:NO) and then I tried to debug my code again but got another error message :

1>------ Build started: Project: Someproject, Configuration: Debug Win32 ------
1>project1.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>c:\users\anne\documents\visual studio 2010\Projects\Someproject\Debug\Someproject.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How can I fix it?

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <math.h>

volatile int counter = 0;

int isPrime(int n)
{
    for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}

unsigned int __stdcall mythread(void*) 
{
    char* s;
    while (counter < 25) {
        int number = counter++;
        s = "No";
        if(isPrime(number)) s = "Yes";
        printf("Thread %d value = %d is prime = %s\n",
            GetCurrentThreadId(), number, s);
    }
    return 0;
}

int main(int argc, char* argv[])
{
    HANDLE myhandleA, myhandleB;
    myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
    myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);

    WaitForSingleObject(myhandleA, INFINITE);
    WaitForSingleObject(myhandleB, INFINITE);

    CloseHandle(myhandleA);
    CloseHandle(myhandleB);

    getchar();

    system("pause");
    return 0;
} 
paulsm4
  • 114,292
  • 17
  • 138
  • 190
cherry_red
  • 309
  • 2
  • 5
  • 11

1 Answers1

2

The basic problem is that you somehow specified "windows app" in your project settings. You want "console app" instead.

Windows apps use "WinMain()"; console apps use "main()".

Look at this link for details:

error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

See also:

Building Console Applications

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Yes I did use windows app because I though that if I make my project in win32 I have to use windows app. – cherry_red Oct 08 '13 at 19:48
  • Nope - that's absolutely not the case. *Every* app (cmd-line/console or Windows GUI) is a "win32" app, and can freely make Win32 calls. – paulsm4 Oct 08 '13 at 19:50
  • I debugged the code in Console Application instead of Win32 Console app and now it gave me another error message: 1>------ Build started: Project: Project1Threads, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> AssemblyInfo.cpp 1> Project1Threads.cpp 1> Generating Code... 1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp 1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== – cherry_red Oct 08 '13 at 20:08
  • SUGGESTION: Failing all else: 1) Create a new, empty project, 2) Specify "Console Mode", 3) Copy your existing source. 4) Try building. The new project should build and run fine. IMHO... PS: You should really use [`CreateThread()`](http://www.codeproject.com/Articles/13557/Creating-Threads-using-the-CreateThread-API) and friends, not "_beginthreadex()": http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516%28v=vs.85%29.aspx – paulsm4 Oct 08 '13 at 22:12