Why can't I compile HelloWorld in C++?

后端 未结 5 1480
北恋
北恋 2021-01-14 02:54

I\'m trying to compile a simple Hello World program in C++ but I keep getting the following error...why?

gcc -o HelloWorldCompiled HelloWorld.cc
/tmp/ccvLW1e         


        
相关标签:
5条回答
  • 2021-01-14 03:07

    You have to use g++, not gcc. It seems that gcc understands that it's C++ (so it doesn't give syntax errors), but "forgets" to link it to the C++ standard library.

    0 讨论(0)
  • 2021-01-14 03:15

    Try using g++ to compile your C++ code, instead of gcc, which is used for compiling C programs.

    0 讨论(0)
  • 2021-01-14 03:18

    Or you could still use gcc but explicitly link with the c++ library thusly:

    gcc -o HelloWorldCompiled HelloWorld.cc -lstdc++
    
    0 讨论(0)
  • 2021-01-14 03:20

    Use g++ not gcc. gcc is a C compiler, whereas g++ is a C++ compiler.

    g++ -o hwcompiled helloworld.cc
    

    Then to execute your compiled program:

    ./hwcompiled
    
    0 讨论(0)
  • 2021-01-14 03:30

    Compile with g++ instead of gcc.

    0 讨论(0)
提交回复
热议问题