代码之家  ›  专栏  ›  技术社区  ›  D1X

错误“未定义对'std::cout'的引用”

  •  307
  • D1X  · 技术社区  · 10 年前

    例如:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Hola, moondo.\n";
    }
    

    它抛出错误:

    gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
    main.cpp:(.text+0xa): undefined reference to `std::cout'
    main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    main.o: In function `__static_initialization_and_destruction_0(int,int)':
    main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
    main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
    returned 1 exit status make: *** [qs] Error 1
    

    此外,此示例:

    #include <iostream>
    
    int main()
    {
        std::cout << "Hola, moondo.\n";
    }
    

    抛出错误:

    gcc -c main.cpp gcc -o edit main.o  main.o: In function `main':
    main.cpp:(.text+0xa): undefined reference to `std::cout'
    main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
    main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
    main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
    returned 1 exit status make: *** [qs] Error 1
    

    注意:我正在使用 Debian 7 (惠西)。

    6 回复  |  直到 3 年前
        1
  •  424
  •   Peter Mortensen icecrime    3 年前

    使用以下工具编译程序:

    g++ -Wall -Wextra -Werror -c main.cpp -o main.o
         ^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
    

    cout 存在于C++标准库中 显式链接 具有 -lstdc++ 使用时 gcc ; g++ 默认情况下链接标准库。

    具有 全球通信中心 , ( 克++ 应优先于 全球通信中心 )

    gcc main.cpp -lstdc++ -o main.o
    
        2
  •  73
  •   shauryachats A B    9 年前

    是,使用 g++ 命令对我有效:

    g++ my_source_code.cpp
    
        3
  •  8
  •   Peter Mortensen icecrime    3 年前

    假设 code.cpp 是源代码,以下内容不会引发错误:

    make code
    ./code
    

    在这里,第一个命令编译代码并创建一个同名的可执行文件,第二个命令运行它 g++ 关键字。

        4
  •  6
  •   iggy12345    5 年前

    生成文件

    如果你正在处理一个makefile,而你最终像我一样来到了这里,那么这可能就是你想要的或者:

    如果您使用的是makefile,则需要更改 cc 如下所示

    my_executable : main.o
        cc -o my_executable main.o
    

    CC = g++
    
    my_executable : main.o
        $(CC) -o my_executable main.o
    
        5
  •  1
  •   Serendipity    3 年前

    在CMake中添加以下行使gcc与std链接,从而识别std::cout

    target_link_libraries(your_project
            PRIVATE
            -lstdc++
            )
    
        6
  •  0
  •   Peter Mortensen icecrime    3 年前

    FWIW,如果你想要一个makefile,下面是如何通过在顶部切换编译器来实现这两个答案。

    # links stdc++ library by default
    # CC := g++
    # or
    CC := cc
    
    all: hello
    
    util.o: util.cc
            $(CC) -c -o util.o  util.cc
    
    main.o: main.cc
            $(CC) -c -o main.o  main.cc
    
    # notice -lstd++ is after the .o files
    hello: main.o util.o
            $(CC) -o hello main.o util.o -lstdc++
    
    clean:
            -rm util.o main.o hello