i have been trying to run a c++ file and i keep getting errors thrown back when i try to run the c++ file, can someone tell me why?
my project is:https://glitch.com/edit/#!/html-db?path=main.cpp:6:1
i have been trying to run a c++ file and i keep getting errors thrown back when i try to run the c++ file, can someone tell me why?
my project is:https://glitch.com/edit/#!/html-db?path=main.cpp:6:1
Hey @Jonyk56,
You see Glitch does not support CPP. Even if it does, the support team of the Glitch may not be able to help.
With that being said, can you provide the error, the code is throwing?
Hey @Jonyk56, C++ should work just as well in a Glitch container as it does in a base Ubuntu Xenial installation, can you let us know what commands youâre running and what errors youâre seeing?
As @chroventer notes, Glitch doesnât âofficiallyâ support C++, but weâll do what we can to help you out.
clang -pthread -std=c++11 -o main main.cpp
@cori @chroventer
/tmp/main-a3d4d9.o: In function main': main.cpp:(.text+0xa): undefined reference to
std::coutâ
main.cpp:(.text+0x1d): 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*)' /tmp/main-a3d4d9.o: In function
__cxx_global_var_initâ:
main.cpp:(.text.startup+0x13): undefined reference to std::ios_base::Init::Init()' main.cpp:(.text.startup+0x19): undefined reference to
std::ios_base::Init::~Init()â
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Hey, fellow Glitcher!
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout
is present in the C++ standard library, which would need explicit linking with -lstdc++
when using gcc
; g++
links the standard library by default.
With gcc
, ( g++
should be preferred over gcc
)
gcc main.cpp -lstdc++ -o main.o
Hello @Jonyk56,
C and C++ have the concept of a âcompilation unitâ, this is the source file which the compiler was invoked with and all of the code that it #include
s.
GCC, MSVC and most other compilers will generate an intermediate âobject fileâ (.o or .obj) for each compilation unit. These must then be combined together, along with any libraries, to form the final executable, in a step referred to as âlinkingâ.
With GCC there are a few ways to do this. Single command:
g++ -o app.exe file1.cpp file2.cpp
This compiles file1.cpp and file2.cpp separately (each is still a single compilation unit) to object files and then links the resulting object files to an executable called âapp.exeâ (you donât need the .exe extension, Iâm just using it for clarity)
Or multiple commands:
g++ -o file1.o -c file1.cpp
g++ -o file2.o -c file2.cpp
g++ -o app.exe file1.o file2.o
This performs the compilation step on each cpp individually and then performs a separate link step with âg++â acting as a front-end to each step. Note the -c
in the first two lines, which tells it you want to compile C/C++ source to .object. The 3rd line, the compiler front-end recognizes youâre asking it to compile object files, and figures out you want to do the link step.
The first method is often easiest for small projects, the second method is useful when you are using any kind of build system and things start to get complicated.
Good luck & hope it helps you out!
sure did, thank you very much!