web-dev-qa-db-ja.com

エラーLNK2019:関数___tmainCRTStartupで参照されている未解決の外部シンボル_main

何が問題なのかわかりません。エラーの場所がわかりません。実装をコメントアウトしてもエラーは解決されません。

ヘッダーファイル

#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

ソース

#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];
    }

}
57
Caleb Jares

プロジェクトにmain()メソッドがある場合でも、リンカーが混乱することがあります。 Visual Studio 2010でこの問題を解決するには、

プロジェクト->プロパティ->構成プロパティ->リンカ->システム

SubSystemをConsoleに変更します。

72
Caleb Jares

この問題もありました。同僚が解決策を見つけました。サードパーティのライブラリヘッダーの「main」の再定義であることが判明しました。

#define main    SDL_main

そのため、解決策は以下を追加することでした:

#undef main

メイン関数の前。

これは明らかに愚かです!

36
Anton Andreev

プロジェクトに_tmain関数がある場合、include <tchar.h>.する必要があります

22
engf-010

main()関数が必要なので、プログラムはどこから始めればよいかがわかります。

16
James McNellis

誰かが明らかなことを見逃した場合に備えて。 GUIアプリケーションを構築して使用する場合
-subsystem:windows"link-argsでは、アプリケーションエントリは WinMain @ 16。 main()ではありません。したがって、このスニペットを使用して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);
}
9
G. Vanem

main()関数を実装しましたか?

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

[編集]

別のソースファイルにmain()があるので、おそらくプロジェクトに追加するのを忘れているでしょう。

既存のソースファイルを追加するには:Solution Explorerで、Source Filesフォルダー、Addをポイントし、Existing Itemをクリックします。 main()を含むソースファイルを選択します

6
ssmir

Visual Studioを使用している場合。このエラーを受け取っているのは、最初に新しいヘッダーfile.hを作成し、main()関数を配置したfile.cppに名前を変更したためかもしれません。

この問題を修正するには、file.cppを右クリックし、[プロパティ]をクリックします。
構成プロパティ->一般->アイテムタイプおよび値をC/C++ヘッダーではなくC/C++コンパイラに変更します。

6
user5632040

私はこの問題を抱えていました:

  • main();そして
  • ソリューション内の他のすべてのプロジェクトを静的ライブラリとして構成します。

私の最終的な修正は次のとおりです。

  • 私のmain()は名前空間にあったので、事実上something::main()と呼ばれていました...この名前空間を削除すると問題が修正されました。
3
Daniel Timms

Visual Studio 2013でDLLプロジェクトの作業中にLNK2019エラーが発生しました。

プロジェクトに新しい構成を追加しました。しかし、「構成タイプ」を「動的ライブラリ」として持つ代わりに、Visual Studioはそれを「アプリケーション」として追加しました。これにより、LNK2019エラーが発生しました。

[プロジェクト]-> [プロパティ]-> [構成プロパティ]-> [全般]に移動し、[構成タイプ]を[動的ライブラリ(.dll)]に、[ターゲット拡張子]を[.dll]に変更して、LNK2019エラーを修正しました。

はい、元の質問はコンソール/アプリケーションプロジェクトについて述べていますが、これは私の答えとは異なる問題です。しかし、この答えを追加することは、このスレッドにつまずく人(私のような人)を助けるかもしれないと信じています。

3
Antony

プログラムのエントリポイントとなるはずのメイン関数がないようです。

0