Меню

Ошибка lnk2001 неразрешенный внешний символ maincrtstartup

I’m learning Assembly at my university, and we were given a CD with MASM 615 on it, and we’re using the Irvine32 include library. Everything works fine with it on the school computer, but when I try to compile and run the same code on my home computer, I get a link error.

INCLUDE Irvine32.inc

.code
main PROC

mov eax,10000h      ; EAX = 10000h
add eax,40000h      ; EAX = 50000h
sub eax,20000h      ; EAX = 30000h
call DumpRegs

exit
main ENDP
END main

This code works fine on the PC at school. At home, I go into DOS, set the path to the MASM folder, and do Make32 file.

This is the error I get:

LINK32 : error LNK2001: unresolved external symbol _mainCRTStartup
test.exe : fatal error LNK1120: 1 unresolved externals

The program compiles (I get the .lst, .obj, and .pdb files), but that’s it. I’m thinking it’s because I have a 64-bit operating system at home, but I have zero idea how to get this up and running in a 64-bit enviornment — the CD or the book has nothing on 64-bit systems. There’s only a make16 or make32 .bat file. It’s a real bummer because that means I can’t do any work at home, unless there’s a work around?

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Linker Tools Error LNK2001

Linker Tools Error LNK2001

10/22/2021

LNK2001

LNK2001

dc1cf267-c984-486c-abd2-fd07c799f7ef

unresolved external symbol «symbol«

The compiled code makes a reference or call to symbol. The symbol isn’t defined in any libraries or object files searched by the linker.

This error message is followed by fatal error LNK1120. To fix error LNK1120, first fix all LNK2001 and LNK2019 errors.

There are many ways to get LNK2001 errors. All of them involve a reference to a function or variable that the linker can’t resolve, or find a definition for. The compiler can identify when your code doesn’t declare a symbol, but not when it doesn’t define one. That’s because the definition may be in a different source file or library. If your code refers to a symbol, but it’s never defined, the linker generates an error.

What is an unresolved external symbol?

A symbol is the internal name for a function or global variable. It’s the form of the name used or defined in a compiled object file or library. A global variable is defined in the object file where storage is allocated for it. A function is defined in the object file where the compiled code for the function body is placed. An external symbol is one referenced in one object file, but defined in a different library or object file. An exported symbol is one that’s made publicly available by the object file or library that defines it.

To create an application or DLL, every symbol used must have a definition. The linker must resolve, or find the matching definition for, every external symbol referenced by each object file. The linker generates an error when it can’t resolve an external symbol. It means the linker couldn’t find a matching exported symbol definition in any of the linked files.

Compilation and link issues

This error can occur:

  • When the project is missing a reference to a library (.LIB) or object (.OBJ) file. To fix this issue, add a reference to the required library or object file to your project. For more information, see lib Files as linker input.

  • When the project has a reference to a library (.LIB) or object (.OBJ) file that in turn requires symbols from another library. It may happen even if you don’t call functions that cause the dependency. To fix this issue, add a reference to the other library to your project. For more information, see Understanding the classical model for linking: Taking symbols along for the ride.

  • If you use the /NODEFAULTLIB or /Zl options. When you specify these options, libraries that contain required code aren’t linked into the project unless you’ve explicitly included them. To fix this issue, explicitly include all the libraries you use on the link command line. If you see many missing CRT or Standard Library function names when you use these options, explicitly include the CRT and Standard Library DLLs or library files in the link.

  • If you compile using the /clr option. There may be a missing reference to .cctor. For more information on how to fix this issue, see Initialization of mixed assemblies.

  • If you link to the release mode libraries when building a debug version of an application. Similarly, if you use options /MTd or /MDd or define _DEBUG and then link to the release libraries, you should expect many potential unresolved externals, among other problems. Linking a release mode build with the debug libraries also causes similar problems. To fix this issue, make sure you use the debug libraries in your debug builds, and retail libraries in your retail builds.

  • If your code refers to a symbol from one library version, but you link a different version of the library. Generally, you can’t mix object files or libraries that are built for different versions of the compiler. The libraries that ship in one version may contain symbols that can’t be found in the libraries included with other versions. To fix this issue, build all the object files and libraries with the same version of the compiler before linking them together. For more information, see C++ binary compatibility between Visual Studio versions.

  • If library paths are out of date. The Tools > Options > Projects > VC++ Directories dialog, under the Library files selection, allows you to change the library search order. The Linker folder in the project’s Property Pages dialog box may also contain paths that could be out of date.

  • When a new Windows SDK is installed (perhaps to a different location). The library search order must be updated to point to the new location. Normally, you should put the path to new SDK include and lib directories in front of the default Visual C++ location. Also, a project containing embedded paths may still point to old paths that are valid, but out of date. Update the paths for new functionality added by the new version that’s installed to a different location.

  • If you build at the command line, and have created your own environment variables. Verify that the paths to tools, libraries, and header files go to a consistent version. For more information, see Use the MSVC toolset from the command line.

Coding issues

This error can be caused by:

  • Mismatched case in your source code or module-definition (.def) file. For example, if you name a variable var1 in one C++ source file and try to access it as VAR1 in another, this error is generated. To fix this issue, use consistently spelled and cased names.

  • A project that uses function inlining. It can occur when you define the functions as inline in a source file, rather than in a header file. Inlined functions can’t be seen outside the source file that defines them. To fix this issue, define the inlined functions in the headers where they’re declared.

  • Calling a C function from a C++ program without using an extern "C" declaration for the C function. The compiler uses different internal symbol naming conventions for C and C++ code. The internal symbol name is what the linker looks for when resolving symbols. To fix this issue, use an extern "C" wrapper around all declarations of C functions used in your C++ code, which causes the compiler to use the C internal naming convention for those symbols. Compiler options /Tp and /Tc cause the compiler to compile files as C++ or C, respectively, no matter what the filename extension is. These options can cause internal function names different from what you expect.

  • An attempt to reference functions or data that don’t have external linkage. In C++, inline functions and const data have internal linkage unless explicitly specified as extern. To fix this issue, use explicit extern declarations on symbols referred to outside the defining source file.

  • A missing function body or variable definition. This error is common when you declare, but don’t define, variables, functions, or classes in your code. The compiler only needs a function prototype or extern variable declaration to generate an object file without error, but the linker can’t resolve a call to the function or a reference to the variable because there’s no function code or variable space reserved. To fix this issue, make sure to define every referenced function and variable in a source file or library you link.

  • A function call that uses return and parameter types or calling conventions that don’t match the ones in the function definition. In C++ object files, Name decoration encodes the calling convention, class or namespace scope, and return and parameter types of a function. The encoded string becomes part of the final decorated function name. This name is used by the linker to resolve, or match, calls to the function from other object files. To fix this issue, make sure the function declaration, definition, and calls all use the same scopes, types, and calling conventions.

  • C++ code you call, when you include a function prototype in a class definition, but don’t include the implementation of the function. To fix this issue, be sure to provide a definition for all class members you call.

  • An attempt to call a pure virtual function from an abstract base class. A pure virtual function has no base class implementation. To fix this issue, make sure all called virtual functions are implemented.

  • Trying to use a variable declared within a function (a local variable) outside the scope of that function. To fix this issue, remove the reference to the variable that isn’t in scope, or move the variable to a higher scope.

  • When you build a Release version of an ATL project, producing a message that CRT startup code is required. To fix this issue, do one of the following,

    • Remove _ATL_MIN_CRT from the list of preprocessor defines to allow CRT startup code to be included. For more information, see General property page (Project).

    • If possible, remove calls to CRT functions that require CRT startup code. Instead, use their Win32 equivalents. For example, use lstrcmp instead of strcmp. Known functions that require CRT startup code are some of the string and floating point functions.

Consistency issues

There’s currently no standard for C++ name decoration between compiler vendors, or even between different versions of the same compiler. Object files compiled with different compilers may not use the same naming scheme. Linking them can cause error LNK2001.

Mixing inline and non-inline compile options on different modules can cause LNK2001. If a C++ library is created with function inlining turned on (/Ob1 or /Ob2) but the corresponding header file describing the functions has inlining turned off (no inline keyword), this error occurs. To fix this issue, define the functions inline in the header file you include in other source files.

If you use the #pragma inline_depth compiler directive, make sure you’ve set a value of 2 or greater, and make sure you also use the /Ob1 or /Ob2 compiler option.

This error can occur if you omit the LINK option /NOENTRY when you create a resource-only DLL. To fix this issue, add the /NOENTRY option to the link command.

This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain. To fix this issue, make sure you match the options to the project type. For more information on these options and entry points, see the /SUBSYSTEM and /ENTRY linker options.

Exported .def file symbol issues

This error occurs when an export listed in a .def file isn’t found. It could be because the export doesn’t exist, is spelled incorrectly, or uses C++ decorated names. A .def file doesn’t take decorated names. To fix this issue, remove unneeded exports, and use extern "C" declarations for exported symbols.

Use the decorated name to find the error

The C++ compiler and linker use Name Decoration, also known as name-mangling. Name decoration encodes extra information about the type of a variable in its symbol name. The symbol name for a function encodes its return type, parameter types, scope, and calling convention. This decorated name is the symbol name the linker searches for to resolve external symbols.

A link error can result if the declaration of a function or variable doesn’t exactly match the definition of the function or variable. That’s because any difference becomes part of the symbol name to match. The error can happen even if the same header file is used in both the calling code and the defining code. One way it may occur is if you compile the source files by using different compiler flags. For example, if your code is compiled to use the __vectorcall calling convention, but you link to a library that expects clients to call it using the default __cdecl or __fastcall calling convention. In this case, the symbols don’t match because the calling conventions are different.

To help you find the cause, the error message shows you two versions of the name. It displays both the «friendly name,» the name used in source code, and the decorated name (in parentheses). You don’t need to know how to interpret the decorated name. You can still search for and compare it with other decorated names. Command-line tools can help to find and compare the expected symbol name and the actual symbol name:

  • The /EXPORTS and /SYMBOLS options of the DUMPBIN command-line tool are useful here. They can help you discover which symbols are defined in your .dll and object or library files. You can use the symbols list to verify that the exported decorated names match the decorated names the linker searches for.

  • In some cases, the linker can only report the decorated name for a symbol. You can use the UNDNAME command-line tool to get the undecorated form of a decorated name.

Additional resources

For more information, see the Stack Overflow question «What is an undefined reference/unresolved external symbol error and how do I fix it?».

  • Remove From My Forums
  • Вопрос

  • —— Build started: Project: vball, Configuration: Debug Win32 ——

    1>Build started 24-May-11 11:12:32 AM.

    1>InitializeBuildStatus:

    1> Touching «Debugvball.unsuccessfulbuild».

    1>ManifestResourceCompile:

    1> All outputs are up-to-date.

    1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup

    1>c:userssvdocumentsvisual studio 2010ProjectsvballDebugvball.exe : fatal error LNK1120: 1 unresolved externals

    1>

    1>Build FAILED.

    1>

    1>Time Elapsed 00:00:02.64

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    code compiled was

    #include<iostream>

    using namespace
    std;

    int
    main()

    {

    cout<<«Hello
    !»;

    return
    0;

    }

    i
    even tried compiling using vs command prompt but even there also following errors occured

    C:program
    files(x86)Microsoft visual studio 10.0VCINCLUDEcmath(19) : error c2061 : syntax error : identifier ‘acosf’;

    like
    above more than 100 errors occured.

Ответы

  • From the description of your issue, I assume, you created an empty project.
    If you want to build a c++ project from scratch do:
    In the project wizard go to Projects Types and check Empty Project option (for me its under Visual C++ General). Important: no clr support). Add a file say vball.cpp to your project (because compiler then uses c++, which you need, when using cout. When file
    is ending with plain c, VS thinks, it is only c-code, and you get these acosf errors). Copy your code into this file. Try a new build and run.

    But for beginners, it is far better, to use the wizards to build new projects, in your case, a console-application would be a good choice. Also you ended up with your thread in VS-Debugger Forum which handles debugger problems. For issues like yours, either

    Visual C++ General or Visual C++ Language
    http://social.msdn.microsoft.com/Forums/en-US/category/visualc
    are much better suited.

    with kind regards

    • Помечено в качестве ответа

      2 июня 2011 г. 8:42

  • Remove From My Forums
  • Question

  • I’m a newbie working through Horton’s Beginning Visual C++.  I’ve been at it for several months.  Last week I got two (related?) errors resuting in a failure to build.

    Horton has us setting Project Properties General Characterset to «Not Set».  We start off with Win32 Console empty project with Precompiled headers.  It has worked fine for months until last week.  I can’t even build a «hello world» 1
    line project in VC++ 2010, now.

    I’ve tried two things I found in the forums:

     1.  Set Character Set to Unicode.  (this eliminates the AlwaysCreate is true error but not the fatal LNK2001→LNK1120 error.  Just to repeat, up until last week this was NOT giving me any problems.

    2.  I also tried adding a manifest validate line to the manifest command line.  No joy.

    Finally, this morning I downloaded and reinstalled Visual Studio in hopes that that would work.  Nope.

    I would appreciate anyone’s ideas on how to solve this.  Win 7 (64bit) Visual Studio 2010 Ver 10.0.30319.1

    1>—— Build started: Project: Test2, Configuration: Debug Win32 ——

    1>Build started 3/13/2011 6:02:05 PM.

    1>InitializeBuildStatus:

    1> Touching «DebugTest2.unsuccessfulbuild».

    1>ManifestResourceCompile:

    1> All outputs are up-to-date.

    1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup

    1>c:usersdaviddocumentsvisual studio 2010ProjectsTest2DebugTest2.exe : fatal error LNK1120: 1 unresolved externals

    1>

    1>Build FAILED.

    • Moved by

      Monday, March 14, 2011 8:46 AM
      move to VC++ forum for better support. (From:Visual Studio Debugger)

Answers

  • Hi,

    According to your description, I think you have a issue about build your C++ project.

    Could you please follow these steps.

    1. Click File menu and choose the Add->project.

    2. Expand the Visual C++ node, select Win32, and click Win32 Console Application (please make sure that you click this option).

    3. Input the name and click on OK.

    4. Make sure that Application type is Console Application , do not change anything, and select Finish.

    5. There is a _tmain function in the cpp file which has the same name with the project.

    6. Build and Start your project or Click F5.

    If your project cannot build or run successfully, please check your configurations of Visual Studio.

    On the other hand  I have followed your steps in my Visual Studio 2010. However it built successfully.  Here is my project and files.

    http://cid-ae2442667686801e.office.live.com/self.aspx/.Documents/T1.rar

    Could you build and run the project in your Visual Studio? If it cannot , maybe there are some  configuration issues in your computer or Visual Studio. 
    Please try to reinstall your windows or Visual Studio. If they still have issue , feel free and let me know.

    Best Regards,

    Rob


    Rob Pan [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by
      Rob Pan
      Monday, March 21, 2011 2:31 AM

  • Remove From My Forums
  • Question

  • I’m a newbie working through Horton’s Beginning Visual C++.  I’ve been at it for several months.  Last week I got two (related?) errors resuting in a failure to build.

    Horton has us setting Project Properties General Characterset to «Not Set».  We start off with Win32 Console empty project with Precompiled headers.  It has worked fine for months until last week.  I can’t even build a «hello world» 1
    line project in VC++ 2010, now.

    I’ve tried two things I found in the forums:

     1.  Set Character Set to Unicode.  (this eliminates the AlwaysCreate is true error but not the fatal LNK2001→LNK1120 error.  Just to repeat, up until last week this was NOT giving me any problems.

    2.  I also tried adding a manifest validate line to the manifest command line.  No joy.

    Finally, this morning I downloaded and reinstalled Visual Studio in hopes that that would work.  Nope.

    I would appreciate anyone’s ideas on how to solve this.  Win 7 (64bit) Visual Studio 2010 Ver 10.0.30319.1

    1>—— Build started: Project: Test2, Configuration: Debug Win32 ——

    1>Build started 3/13/2011 6:02:05 PM.

    1>InitializeBuildStatus:

    1> Touching «DebugTest2.unsuccessfulbuild».

    1>ManifestResourceCompile:

    1> All outputs are up-to-date.

    1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup

    1>c:usersdaviddocumentsvisual studio 2010ProjectsTest2DebugTest2.exe : fatal error LNK1120: 1 unresolved externals

    1>

    1>Build FAILED.

    • Moved by

      Monday, March 14, 2011 8:46 AM
      move to VC++ forum for better support. (From:Visual Studio Debugger)

Answers

  • Hi,

    According to your description, I think you have a issue about build your C++ project.

    Could you please follow these steps.

    1. Click File menu and choose the Add->project.

    2. Expand the Visual C++ node, select Win32, and click Win32 Console Application (please make sure that you click this option).

    3. Input the name and click on OK.

    4. Make sure that Application type is Console Application , do not change anything, and select Finish.

    5. There is a _tmain function in the cpp file which has the same name with the project.

    6. Build and Start your project or Click F5.

    If your project cannot build or run successfully, please check your configurations of Visual Studio.

    On the other hand  I have followed your steps in my Visual Studio 2010. However it built successfully.  Here is my project and files.

    http://cid-ae2442667686801e.office.live.com/self.aspx/.Documents/T1.rar

    Could you build and run the project in your Visual Studio? If it cannot , maybe there are some  configuration issues in your computer or Visual Studio. 
    Please try to reinstall your windows or Visual Studio. If they still have issue , feel free and let me know.

    Best Regards,

    Rob


    Rob Pan [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by
      Rob Pan
      Monday, March 21, 2011 2:31 AM

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Ошибка lo на электронных весах vitek
  • Ошибка lnk2001 неразрешенный внешний символ main