twieg
twieg

Reputation: 73

g++ outputs filenames with leading space?

I'm building my first c++ program in VS Code via g++, but when I specify a filename for the output executable with the -o filename.exe flag, the output files always have a leading space (" filename.exe" rather than just "filename.exe").

I'm just getting started with c++, and I've never used VS Code before (but chose it because it seems like a pretty popular "IDE" for c++ stuff), so bear with me if this is stupid. I'm running this all on Kubuntu 18.04 with the latest version of build-essential (12.4ubuntu1) and VS Code (1.30.2).

Based on a little bit of google-fu (and some trial/error), I put together this build task tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "${workspaceRoot}/helloworld.cpp", "-o helloworld.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }

        }
    ]
}

When I run this to build my helloworld.cpp file (which is exactly what you expect), it outputs an executable file called ' helloworld.exe', rather than helloworld.exe.

Upvotes: 0

Views: 122

Answers (1)

tkausl
tkausl

Reputation: 14269

There are generally two ways to pass to arguments to command line options. The first one is as a single value:

<command> -ohelloworld.exe

the second is as two values:

<command> -o helloworld.exe

The parser understands both and they have the same result. Unfortunately in your example, you're not passing two values but one with a space in it, as there is no shell in between which could split them at the space. So what gcc sees is literally a single argument with the value -o helloworld.exe, and since the -o is followed by more characters, gcc takes it as the filename, including the leading space. So to fix this, either use the single argument "ohelloworld.exe" or split them into two "-o", "helloworld.exe".

Upvotes: 1

Related Questions