69

I don't know what's wrong with it.. I can't find where the error is, commenting out the implementation doesn't resolve the error either.

Header File

#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>  // Provides size_t

namespace main_savitch_3
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef std::size_t size_type;
        static const size_type CAPACITY = 30;
        // CONSTRUCTOR
        sequence( );
        // MODIFICATION MEMBER FUNCTIONS
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const;
        bool is_item( ) const;
        value_type current( ) const;
    private:
        value_type data[CAPACITY];
        size_type used;
        size_type current_index;
    };
}

#endif

Source

#include "sequence1.h"
#include <assert.h>

namespace main_savitch_3
{

    // Default constructer - sequence is empty
    sequence::sequence()
    {
        used = current_index = 0;
    }


    // Start the iteration
    void sequence::start()
    {
        current_index = 0;
    }
    // Iterate
    void sequence::advance()
    {
        current_index++;
    }


    // Number of items in the sequence
    sequence::size_type sequence::size() const
    {
        return used;
    }
    // Checks if there is a current item
    bool sequence::is_item() const
    {
        return current_index <= used && used > 0;
    }
    // Returns the current value
    sequence::value_type sequence::current() const
    {
        assert(is_item()); // no current item
        return data[current_index];
    }


    // Adds an item BEFORE the current index
    void sequence::insert(const value_type& entry)
    {
        assert(entry != 0); // pointer is invalid
        assert(current_index < sequence::CAPACITY); // no room to add an item

        // move items up - starting with the last item and working down to the current item
        // arrays start at 0, so the -1 adjusts it
        for (size_type i = used - 1; i >= current_index; i--)
            data[i + 1] = data[i];

        data[current_index] = entry;
    }
    // Adds an item AFTER the current index
    void sequence::attach(const value_type& entry)
    {
        assert(entry != 0); // pointer is invalid
        assert(current_index < sequence::CAPACITY); // no room to add an item

        // move items up - starting with the last item and working down to the current item
        // arrays start at 0, so the -1 adjusts it
        for (size_type i = used - 1; i > current_index; i--)
            data[i + 1] = data[i];

        if (current_index = 0)
            data[used] = entry;
        else
            data[current_index + 1] = entry;
    }
    // Removes the current item
    void sequence::remove_current()
    {
        for (size_type i = current_index; i < used; i++)
            data[i] = data[i + 1];
    }

}
Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
  • 8
    This is a **link** error. It looks like you're trying to build an executable without a `main()` function ? You should either be building a library, or else you need a source file with a `main()` in it. – Paul R Jan 30 '11 at 20:46
  • Nitpick: If you want `size_t`, you should use `` rather than ``. – Billy ONeal Jan 30 '11 at 20:51
  • @Billy: `size_t` is defined in both headers. – James McNellis Jan 30 '11 at 20:54
  • I have a main function in another file, it is a test program provided by the textbook. int main() {...} I don't know what I have to do to make the program recognize it – Caleb Jares Jan 30 '11 at 22:20
  • 1
    @James: Yes, but `` is "smaller" :) – Billy ONeal Jan 31 '11 at 01:28
  • 1
    I had similar issue caused minor ignorance, didn't select x64 build while lib included was x64. – Pervez Alam Jan 17 '17 at 16:34
  • Thanks. I am doing assembly tutorials. Had an empty source.cpp besides source.asm with the code. Just adding int main etc to the source.cpp file did the job of getting rid og the error and fatal error. But now it is not running the source.asm file? – mortenlund Aug 26 '22 at 13:41

17 Answers17

88

Even if your project has a main() method, the linker sometimes gets confused. You can solve this issue in Visual Studio 2010 by going to

Project -> Properties -> Configuration Properties -> Linker -> System

and changing SubSystem to Console.

Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
51

We also had this problem. My colleague found a solution. It turned up to be a redefinition of "main" in a third party library header:

#define main    SDL_main

So the solution was to add:

#undef main

before our main function.

This is clearly a stupidity!

Anton Andreev
  • 2,052
  • 1
  • 22
  • 23
  • Thanks, that was the culprit for me too! It is a better solution to initialize SDL properly though. – Csq Jul 26 '15 at 20:20
  • Huh. this just worked for me, too. Using SDL2.0.3 and GLEW1.13.0. – Nick Desaulniers Sep 09 '15 at 05:22
  • 2
    Oh, in SDL_main.h, there's a comment about "Redefine main() on some platforms so that it is called by SDL." then: #ifdef __WIN32__ #def SDL_MAIN_AVAILABLE #endif ... #if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) #define main SDL_main #endif As @Csq said, it looks like there's a better way to initialize SDL2. – Nick Desaulniers Sep 09 '15 at 05:46
  • 1
    @Nick Desaulniers Just fought through this issue and resolved as per this answer. What is meant by "looks like there is a better way to initialize SDL2" ? Thanks. – RigidBody Aug 31 '16 at 19:34
  • This was exactly my problem! Thanks for figuring this tricky thing out. Writing "#undef main" directly before the real main function solved this linker error – Anonymous Jan 31 '18 at 22:11
  • 4
    Before you `#include `, you may `#define SDL_MAIN_HANDLED`. – Mike Jun 28 '18 at 15:45
  • Although including #undef main worked, the real issue for me was forgetting to add SDL2main.lib as an additional dependency in the project linker properties. – James Mart May 08 '19 at 17:06
22

if you have _tmain function in your projects you need to include <tchar.h>.

engf-010
  • 3,980
  • 1
  • 14
  • 25
  • I have a main function in another file, it is a test program provided by the textbook. int main() {...} I don't know what I have to do to make the program recognize it – Caleb Jares Jan 30 '11 at 22:24
17

You need a main() function so the program knows where to start.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I have a main function in another file, it is a test program provided by the textbook. int main() {...} I don't know what I have to do to make the program recognize it – Caleb Jares Jan 30 '11 at 22:14
  • Now compile and build the two source files i.e, `g++ main.cpp sequence1.cpp -o result.out` – Mahesh Jan 30 '11 at 22:33
  • I use visual studio 2010, because.. I don't know how to compile by command line. So how do I fix it in visual studio? – Caleb Jares Jan 30 '11 at 23:02
  • 1
    @cable729: Have you added both .cpp files to the same project in Visual Studio? – James McNellis Jan 30 '11 at 23:03
  • 2
    I figured it out, in the project properties, under Linker, it was set to Windows, instead of Console. Thanks for the help – Caleb Jares Jan 30 '11 at 23:28
  • Ahh, there ya go. That one will get ya every time. – Andrew Shelansky Feb 02 '11 at 01:42
  • It happened to me again, and it was a matter of doing: using namespace namespace_of_header_file instead of namespace namespace_of_header_file{int main(){...}} Strange, I don't see much difference. – Caleb Jares Feb 02 '11 at 22:04
  • 2
    @cable729: The `main` function that is the entry point of the program must be in the global namespace. You cannot put it in any other namespace and you cannot use a using directive to bring some other `main` function into the global namespace. – James McNellis Feb 02 '11 at 22:06
8

In case someone missed the obvious; note that if you build a GUI application and use
"-subsystem:windows" in the link-args, the application entry is WinMain@16. Not main(). Hence you can use this snippet to call your main():

#include <stdlib.h>
#include <windows.h>

#ifdef __GNUC__
#define _stdcall  __attribute__((stdcall))
#endif

int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
         struct HINSTANCE__ *hPrevInstance,
         char               *lpszCmdLine,
         int                 nCmdShow)
{
  return main (__argc, __argv);
}

G. Vanem
  • 81
  • 1
  • 1
7

I encountered the LNK2019 error while working on a DLL project in Visual Studio 2013.

I added a new configuration to the project. But instead of having the "Configuration Type" as "Dynamic Library", visual studio added it as "Application". This resulted in the LNK2019 error.

Fixed the LNK2019 error by going to Project -> Properties -> Configuration Properties -> General and changing "Configuration Type" to "Dynamic Library (.dll)" and "Target Extension" to ".dll".

Yes, the original question talks about a console/application project, which is a different problem than my answer. But I believe adding this answer might help someone (like me) that stumbles upon this thread.

HelloWorld101
  • 3,878
  • 2
  • 34
  • 47
  • 2
    Works! Just make sure you are using the correct build configuration. Both Debug and Release in that dialog has to be changed in vs2015. – Edza May 12 '17 at 07:53
  • Also, make sure to add the library path to your solution – CocoCrisp Sep 27 '17 at 09:00
7

If you are using Visual Studio. The reason you might be recieving this error may be because you originally created a new header file.h and then renamed it to file.cpp where you placed your main() function.

To fix the issue right click file.cpp -> click Properties go to
Configuration Properties -> General ->Item Type and change its value to C/C++ compiler instead of C/C++ header.

user5632040
  • 71
  • 1
  • 1
6

I had this problem despite:

  • having a main(); and
  • configuring all other projects in my solution to be static libraries.

My eventual fix was the following:

  • my main() was in a namespace, so was effectively called something::main() ...removing this namespace fixed the problem.
Sufian
  • 6,405
  • 16
  • 66
  • 120
Daniel Timms
  • 61
  • 1
  • 1
6

Did you implement the main() function?

int main(int argc, char **argv) {
    ... code ...
    return 0;
}

[edit]

You have your main() in another source file so you've probably forgotten to add it to your project.

To add an existing source file: In Solution Explorer, right-click the Source Files folder, point to Add, and then click Existing Item. Now select the source file containing the main()

ssmir
  • 1,522
  • 8
  • 10
  • 1
    I have a main function in another file, it is a test program provided by the textbook. int main() {...} I don't know what I have to do to make the program recognize it – Caleb Jares Jan 30 '11 at 22:21
  • @cable729 You probably use Visual C++. Then you should add the file with the `main()` to the project's list of source files. Another option is to copy and paste your `main()` to your sequence1.cpp. – ssmir Jan 30 '11 at 22:50
1

You appear to have no main function, which is supposed to be the entry-point for your program.

Andrew Shelansky
  • 4,932
  • 3
  • 20
  • 19
  • 1
    I have a main function in another file, it is a test program provided by the textbook. int main() {...} I don't know what I have to do to make the program recognize it – Caleb Jares Jan 30 '11 at 22:23
1

In visual studio, project properties, for x86, x64, Release and Debug Configurations

Linker > System > SubSystem

For /SUBSYSTEM:WINDOWS The following main is required (Note this is for the unicode, wide character version):

#include <Windows.h>

int WINAPI  wWinMain(_In_ HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR    lpCmdLine,
    int       nCmdShow)
{
   return 0;
}

For /SUBSYSTEM:CONSOLE The following main is required :

int main(int argc, char* argv[], char* environment[]){
      return 0;
}
Lefteris E
  • 2,806
  • 1
  • 24
  • 23
0

My Problem was something else
I had a method in a class with no implementation.
like this:

class Hello {
    public:
        // ...
        void PrintHelloWorld();
        // ...
};

When I call the method I got a error:

hello().PrintHelloWorld();

Error: LNK2019 unresolved external symbol "public: void __thiscall Hello::PrintHelloWorld(void)" (?PrintHelloWorld@Hello@@QAEXXZ) referenced in function _main demo .\Peyman\source\repos\demo\demo\demo.obj 1

In fact, I use an SDK with no implementation and it causes the error. Please check is there any implementation or not. void PrintHelloWorld(){} with brackets

Peyman Majidi
  • 1,777
  • 2
  • 18
  • 31
0

For me the problem was SDL as well. I moved
// Tell SDL not to mess with main() and #define SDL_MAIN_HANDLED before including the SDL libraries. And the problem seems fixed.

0

My context: Visual Studio

I have had the same issue when I've added an existing file with wrong extension (like *.ccc).

Than I've changed the extension to *.cpp. When I've built the solution I've got a similar link error.

I've discovered the reason when I've taken a look to *.vcxproject file. The file has not been identified by Visual Studio as source file so it put it to ClInclude group (see below).

    <ItemGroup>
        <ClInclude Include="toto.cpp" />
    </ItemGroup>

I move it to ClCompile group (see below) and rebuild without error.

    <ItemGroup>
        <ClCompile Include="AnotherSource.cpp" />
        <ClCompile Include="toto.cpp" />
    </ItemGroup>

CCP
  • 7
  • 4
0

Add #define SDL_MAIN_HANDLED before including <SDL.h>.

However if you are still facing issues (as I was) it may be because your target platform is set to x32 but using SDL dll of x64 (changing the platform to x64 worked for me). If it is set to the wrong architecture then it may cause a linker error due to _main() not being found.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
-2

go to "Project-Properties-Configuration Properties-Linker-input-Additional dependencies" then go to the end and type ";ws2_32.lib".

Abdullah
  • 27
  • 1
-2

try using return 0;

if it keeps failing change your solution platform to 64x instead of 86x and go to configuration manager(that's were you change the 86x to 64x) and in platform set it to 64 bits

that works for me, hope it work to you

Stefan
  • 1