-1

Basically when I click run an error pups up

[Running] cd "c:\Users\alexv\Documents\Playground\" && g++ Hello_World -o c:\Users\alexv\Documents\Playground\Hello_World && "c:\Users\alexv\Documents\Playground\"c:\Users\alexv\Documents\Playground\Hello_World c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:Hello_World: file format not recognized; treating as linker script c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:Hello_World:1: syntax error collect2.exe: error: ld returned 1 exit status

I'm using VSC, MinGW and a PC, the program I'm trying to run is a simple hello world:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello world" << endl;

    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Alex Vera
  • 9
  • 1
  • I am not an expert in Visual Studio Code however you probably need to show your `json` files. – drescherjm Dec 16 '19 at 19:05
  • 5
    `g++ Hello_World`: where is your source file extension (e.g. `.cpp`, `.cxx`, etc.)? – ph3rin Dec 16 '19 at 19:06
  • Related to my first comment: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm Dec 16 '19 at 19:21
  • 2
    I recommended you to use Visual Studio instead of Visual Studio Code. It's much cleaner and easy to use. Also, please don't use `using namespace std;`.[It's considered a bad practice.](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – nakE Dec 16 '19 at 19:26
  • I would rather recommend eclipse or code blocks, both together with mingw. Admitted, getting the IDE running is a bit of a mess, but once set up, both run nicely. I personally consider especially eclipse as superior to MSVC, more important is a privacy issue: With MSVC, you have to register, so you pay with your personal data, eclipse or CB, in contrast, are *really* free... – Aconcagua Dec 16 '19 at 19:41

1 Answers1

4

As mentioned in the comments, the error message is relatively clear.

g++ accepts several different kinds of files as argument. It tries to determine what kind of file you are handing it by looking at the file name extension.

Your file has no extension, so g++ defaults to assuming it is a linker script, but it really is a C++ source file.

Use one of the common file extensions for C++ source files and g++ will handle the file correctly without further options. Common C++ source file name extensions are .cpp, .cc or .cxx.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Tangential question: who thought that treating unrecognized files as linker scripts was a sensible default?! – user253751 Dec 16 '19 at 19:20
  • @user253751 Linux executables/scripts, perhaps? – Ripi2 Dec 16 '19 at 19:24
  • @user253751 I think it is just directly passed to the linker if unrecognized and since the file is not a valid object file, thats the result. – walnut Dec 16 '19 at 19:24
  • I think this is caused by a misconfiguration of visual studio code. Could also possibly be as simple as naming the source file wrongly. Although with that said if the OP has disappeared we may never know. – drescherjm Dec 16 '19 at 19:26
  • Side note: If using no extension (or an unusual one g++ does not recognise), [`-x` option](https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options) allows to explicitly specify the language in which to compile the file... – Aconcagua Dec 17 '19 at 09:58