2

I need to pass the arg -Wl,-Bstatic,--whole-archive to g++.

"version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "shell: g++.exe build active file",
            "command": "C:\\MinGW\\x86\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-Wl,-Bstatic,--whole-archive",
                "-Xlinker",
                "-Map=${fileDirname}\\${fileBasenameNoExtension}.map",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\MinGW\\x86\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

It gives me in output this in the terminal.

Executing task: C:\MinGW\x86\bin\g++.exe -g 'c:\Users\remi\Desktop\OK - VSCode\loaderstack.cpp' -Wl,-Bstatic,--whole-archive -Xlinker '-Map=c:\Users\remi\Desktop\OK - VSCode\loaderstack.map' -o 'c:\Users\remi\Desktop\OK - VSCode\loaderstack.exe' <

    At line:1 char:84
    + ... e -g 'c:\Users\remi\Desktop\OK - VSCode\loaderstack.cpp' -Wl,-Bstatic ...
    +                                                                 ~
    Missing argument in parameter list.
    At line:1 char:93
    + ... Users\remi\Desktop\OK - VSCode\loaderstack.cpp' -Wl,-Bstatic,--whole- ...
    +                                                                 ~
    Missing argument in parameter list.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingArgument

Is there anyway to build inside VSCode with these comma-separated args ?

  • show the command VSC executes in the terminal – rioV8 May 28 '20 at 18:50
  • Thank you for reading. Executes added. – MelectronVolt May 28 '20 at 19:07
  • It looks like maybe powershell is getting in the way. What happens if you change the command to & and move the executable to the first argument? – clcto May 28 '20 at 19:23
  • In my trial on my machine the & did not work as expected; it looks like you need to use the `--%` operator as the first argument to get powershell to not try to interpret the line. See https://stackoverflow.com/a/42383116/2721883 and https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7: *"The stop-parsing symbol (--%), introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting input as PowerShell commands or expressions."* – clcto May 28 '20 at 19:36
  • Works better... But... now there is a problem with the path. In fact there are spaces and powershell doesn't like them... C:\MinGW\x86\bin\g++.exe --% -g 'c:\Users\remi\Desktop\OK - VSCode\loaderstack.cpp' -o "c:\Users\remi\Desktop\OK - VSCode\loaderstack.exe" g++.exe: error: 'c:\Users\remi\Desktop\OK: Invalid argument – MelectronVolt May 28 '20 at 20:06
  • Try to escape `,` with ` like my answer. – Afshin Aug 16 '22 at 08:21

2 Answers2

1

I originally answered this question (like a dumb dumb) as if you were using Linux, so I deleted that answer and included a new one for PowerShell.

This is sort of a common problem when dev's use GCC with Powershell. The problem is that PowerShell is very programmatic in the way it implements its interface, and the way that it executes commands. With Linux, CLI's are all written opensource (mostly) and are developed by the developers, where powershell is created by a company that dictates how every little detail works (there are benefits, and downsides to both). PowerShell has aspects/features (or w/e you want to call them) that just feel like somthing a programming language has, for example PowerShell has scopes, and what you pass to powershell gets parsed according to the context (or scope) that your currently in. The problem you are dealing with is that your command, that your handing GCC through your VS Code v2 Task is not being parsed properly due to the context in which the task is handing it to Power-shell.



YOU HAVE 2 OPTIONS



Option #1

The first option is to use a scope where the parser will correctly interoperate the command.

Remember, your using a VSCode task, powershell & gcc, to make sure communication succeeds across all three, you need to include the scope to use in what you are communicating. To do that we want to make use of the...

Call Operator &

To use the call operator just format the initial command to execute as shown in the code block bellow:

  "command": "& C:\\MinGW\\x86\\bin\\g++.exe",
Where I know that this is a valid solution to your problem, I am currently on a Linux System, I have windows dual booted, but I am too lazy to switch over to it, so just in-case something needs to be tweaked, use the link for the Call Operator I posted above, MS documentation is very good, and very specific about how to implement its software-technologies


Option #2

Your second option takes a totally different route than the first.

Instead of dealing with the scope being the problem, your gonna deal with Power-shell's inability to parse the MingW GCC Command.

To deal with Power-shell's parsing issue, we will tell it to stop parsing the command, henceforth, the...

stop-parsing flag --%

(For the semantics police-type of developers: I think its technically a token, not a flag)

Using the flag looks like this: gcc %--

So the whole command should look somthing like this:

            "args": [
                "--%"
                "-g",
                "${file}",
                "-Wl,-Bstatic,--whole-archive",
                "-Xlinker",
                "-Map=${fileDirname}\\${fileBasenameNoExtension}.map",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
Again, I included the link to the docs for the stop-parsing token above, just in-case something needs to be tweaked.

The example I showed above is somthing I had to use on a project that I worked on for a very long time, because of that experiance, I perfer to use the no

Somthing else that I don't know much about, by I read about when I DDG'd the links to Microsoft-site that might, maybe work is using the Arguments mode, which seems to be similar to the stop parsing command?

Anyhow, here is the link if you want to read about it.

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
0

I should say that I have not tried this approach, but I think it will work.

I suggest that you escape , in powershell using `. Try it in your config file like this:

"-Wl`,-Bstatic`,--whole-archive",

I'm not sure if it works, but since it worked for echo hell`,o`,o, I guess everything will be fine. Please let me know if this approach works.

Afshin
  • 8,839
  • 1
  • 18
  • 53