How to run msvc link.exe from cmd line or system with spaces in library path?

Mate 25 Reputation points
2023-06-19T10:03:57.0333333+00:00

I am trying to run msvc linker (link.exe) from C++ using system(). However, the library path I specify in /libpath: has spaces.

I just read here that:

"On the command line, an option consists of an option specifier, either a dash (-) or a forward slash (/), followed by the name of the option. Option names cannot be abbreviated. Some options take an argument, specified after a colon (:). No spaces or tabs are allowed within an option specification, except within a quoted string in the /COMMENT option."

So it seems it's not possible to have spaces. Did I misunderstood something? If not, what would be the best way to work around this?

Just fyi, I tried a few workarounds like putting quotes around a path or even triple quotes but it didn't work. Something like this:

"path-to-link/link.exe" /out:test.exe test.obj msvcrt.lib /libpath:"""path-to-libs""" /subsystem:console /entry:main

or

"path-to-link/link.exe" /out:test.exe test.obj msvcrt.lib /libpath:"path-to-libs" /subsystem:console /entry:main

If I posted this on a wrong place, feel free to suggest a more appropriate place.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,961 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-06-20T03:09:21.01+00:00

    Hi, @Mate

    You could use \" instead of ".

    \" space path \"

    \"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.37.32705/bin/Hostx64/x64/link.exe\"
    

    Since there is one space between link.exe and /out, you need to add another pair of \".

    " \" \"(space) link.exe \" (space) /out:test.exe \" "

    E.g.

    #include <cstdlib>
    
    int main() {
        const char* command = "\"\"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.37.32705/bin/Hostx64/x64/link.exe\" /out:test.exe \"XXX/ConsoleApplication1.obj\" \"/libpath:C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.37.32705/lib/x64\" \"/libpath:C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/um/x64\" \"/libpath:C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/ucrt/x64\" \" ";
    
        int result = system(command);
        system("test.exe");
        return 0;
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.