2

I'm running C++ on Ubuntu 20.04/g++ 11.1.0 and it seems the program simply isn't being run when I call on the executable. Here is what I am doing:

Code:

/* newtest.cpp */
#include <iostream>

int main() {
   std::cout<< "this works" << std::endl;
   return 0;
}

Compilation:

$ g++ -o test newtest.cpp

Execution:

$ test

And nothing is printed; the program compiles and runs without errors. When I compile it with the -g flag and run it using the GDB debugger it works, but not under normal execution. Older programs on my machine are able to print to the console fine. Any thoughts on why this is happening?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 12
    Not `test`, but `./test`. The former calls either the bash's built-in called `test` or corresponding executable. – yeputons Jul 05 '21 at 21:14
  • Thank you, I knew it had to be something silly like this! Is there any way to make it so I can call this using `test` instead of having to specify `./test`? – astromonkeys Jul 05 '21 at 21:16
  • "Is there any way to make it so I can call this using test instead of having to specify ./test?" - if you want to tell your shell to do that, type `alias test=./test`. It's a bad idea though - `test` may be used by scripts you run, so changing the meaning could break all kinds of things. Better to rename your program to something less generic and more meaningful. – Tony Delroy Jul 05 '21 at 21:20
  • There's a way, but it opens a security hole so I won't ever recommend it. – o11c Jul 05 '21 at 21:20
  • Thanks everyone. Didn't think the executable's name might conflict with something that already exists – astromonkeys Jul 05 '21 at 21:23
  • 2
    It's nothing to do with it conflicting. it is because the "test" in your directory is simply not found, because it's not on the PATH. We are not here to hide info. To run it without the preceding"./" you would add "." to your PATH, but you do NOT want to do that. It is asking for trouble since if "." preceded system binaries you would potentially compromise your system. Figure out why ;) – RichieHH Jul 05 '21 at 21:28
  • 1
    @RichieHH **the "test" in your directory is simply not found** This is not correct. It runs `test`, an embedded shell program that does nothing if no arguments given. It can also be /bin/test if bash is running without embedded programs. – 273K Jul 05 '21 at 21:40

0 Answers0