0
votes

I am trying to execute the following command in irb,

exec 'gcc j.cpp'

but, i got the following error:

/tmp/ccYhafBj.o: In function `main':
j.cpp:(.text+0x14): undefined reference to `std::cout'
j.cpp:(.text+0x19): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
/tmp/ccYhafBj.o: In function `__static_initialization_and_destruction_0(int, int)':
j.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
j.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccYhafBj.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

My file is in the same directory where i opened the irb --simple-prompt to run the file. The contents of my file are:

#include <iostream>
using namespace std;
int main()
{    cout << "Hello World";
    return 0;
}

Can someone tell me where i am wrong, and some way to fix the problem. All i want is to run the j.cpp file and get the output/ errors that come in the process.

1

1 Answers

2
votes

Use the C++ compiler, not the C compiler.

exec 'g++ j.cpp'

exec will replace the current process, so you may want to use backticks instead.

output = `g++ j.cpp -o "prog" && ./prog`

The output file is specifically named as "prog" just to be more clear on what we are executing.