Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings & Kind Regards
I am attempting to execute cl.exe via C++ system(...) call w/ argument shown below:
"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" /JMC /permissive- /ifcOutput "x64/Debug" /GS /W3 /Zc:wchar_t /I"C:/Program Files (x86)/Visual Leak Detector/include" /ZI /Gm- /Od /sdl /Fd"x64/Debug/vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /Fa"x64/Debug/" /EHsc /nologo /Fo"x64/Debug/" /Fp"x64/Debug/IOWIU.pch" /diagnostics:column /c main.cpp

The result is an error as shown below:
calling : system("C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" /JMC /permissive- /ifcOutput "x64/Debug" /GS /W3 /Zc:wchar_t /I"C:/Program Files (x86)/Visual Leak Detector/include" /ZI /Gm- /Od /sdl /Fd"x64/Debug/vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /Fa"x64/Debug/" /EHsc /nologo /Fo"x64/Debug/" /Fp"x64/Debug/IOWIU.pch" /diagnostics:column /c main.cpp)
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
return value is : 1

The same identical command run from the command line results in correct execution as shown below:
C:\Users\Bernard\source\repos\IOWIU>"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" /JMC /permissive- /ifcOutput "x64/Debug" /GS /W3 /Zc:wchar_t /I"C:/Program Files (x86)/Visual Leak Detector/include" /ZI /Gm- /Od /sdl /Fd"x64/Debug/vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /Fa"x64/Debug/" /EHsc /nologo /Fo"x64/Debug/" /Fp"x64/Debug/IOWIU.pch" /diagnostics:column /c main.cpp
main.cpp
#include C:\Users\Bernard\source\repos\bpwSoftware\execution.MACROS.H

C:\Users\Bernard\source\repos\IOWIU>

Below is the relevant code:
string cl_path = "C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe";
string cl_options = "/JMC /permissive- /ifcOutput \"x64/Debug\" /GS /W3 /Zc:wchar_t /I\"C:/Program Files (x86)/Visual Leak Detector/include\" /ZI /Gm- /Od /sdl /Fd\"x64/Debug/vc143.pdb\" /Zc:inline /fp:precise /D \"_DEBUG\" /D \"_CONSOLE\" /D \"_UNICODE\" /D \"UNICODE\" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /Fa\"x64/Debug/\" /EHsc /nologo /Fo\"x64/Debug/\" /Fp\"x64/Debug/IOWIU.pch\" /diagnostics:column /c";

string sspace = " ";
string sdbl_quotes = "\"";
auto left_paren = '(';
auto right_paren = ')';
auto system_arg = sdbl_quotes + cl_path + sdbl_quotes + sspace + cl_options + sspace + "main.cpp";
cout << "calling : system" << left_paren << system_arg << right_paren << endl;
auto result = system(system_arg.c_str());
cout << "return value is : " << result << endl;

The call to system() w/ the original cl_path works w/o error as sdbl_quotes is pre-post-pended to cl_path if only the 1st two options are passed so apparently the problem is w/ the 3rd option i.e. to wit \"x64/Debug/\" as passing it and only the 1st 2 and none of the remaining options results in the usual CMD error i.e.
Shell
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
The original option copied from project Property Pages please note the quotes are part of the option argument : "x64\Debug\" my converted form so as to form string type : \"x64/Debug/\" results in error I do not know how to resolve.

Below is a direct copy/paste of the screen output. As you can see the path to cl.exe is quoted which is precisely what the command processor sees and should see and still results in CMD error. You will also note the /I option argument is also quoted. Please note on the next output the quotes around the /I option argument are removed and the result is no CMD error reported only a compiler error which is expected demonstrating success in calling cl.exe.
Terminal
__cplusplus 199711 _MSVC_LANG 202002
SYSTEM 2022-12-04 03:41:32.3135127
UTC    2022-12-04 03:41:32.3230103
TAI    2022-12-04 03:42:09.3644184
GPS    2022-12-04 03:41:50.3716110
FILE   2022-12-04 03:41:32.3781440
LOCAL  2022-12-03 22:41:32.3945656

calling : system("C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" /permissive- /GS /W3 /Zc:wchar_t /I "C:\Users\Bernard\source\repos\bpwSoftware" /Gm- /Od /sdl /Zc:inline /fp:precise /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /EHsc /nologo /diagnostics:column /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE TEST.cpp)
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
return value is : 1

C:\Users\Bernard\source\repos\IOWIU\x64\Debug\IOWIU.exe (process 14484) exited with code 0.
Press any key to close this window . . .

Terminal
__cplusplus 199711 _MSVC_LANG 202002
SYSTEM 2022-12-04 03:39:47.9385039
UTC    2022-12-04 03:39:47.9444312
TAI    2022-12-04 03:40:24.9793121
GPS    2022-12-04 03:40:05.9859770
FILE   2022-12-04 03:39:47.9899277
LOCAL  2022-12-03 22:39:48.0050586

calling : system("C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" /permissive- /GS /W3 /Zc:wchar_t /I C:\Users\Bernard\source\repos\bpwSoftware /Gm- /Od /sdl /Zc:inline /fp:precise /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++20 /FC /EHsc /nologo /diagnostics:column /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE TEST.cpp)
TEST.cpp
C:\Users\Bernard\source\repos\IOWIU\TEST.cpp(1,9): error C2065: 'z': undeclared identifier
return value is : 2

C:\Users\Bernard\source\repos\IOWIU\x64\Debug\IOWIU.exe (process 14728) exited with code 0.
Press any key to close this window . . .

So in conclusion the problem all along has not been lack of quotes around cl.exe path as they have been present all along as these two above windows clearly demonstrate and has been stated repeatedly but rather the presence of quotes in option arguments as has also been previously stated and clearly demonstrated above.
May I suggest kind readers/advisors/helpers make effort to read/study/understand all text/comments etc. prior to advising/helping/assisting to avoid needless and troublesome repetition.
For further assistance please note the /I option is the 5th such i.e. to wit 1 , 2 , 3 , 4 , 5 ! Bingo! How hard was that. I assume counting is taught in Computer Science classes.
Thank You Kindly

What I have tried:

Read documentation re/ C++ system() , re/ CMD escape characters , re/ long path names which contain spaces.
Posted
Updated 3-Dec-22 17:11pm
v8
Comments
Rick York 3-Dec-22 16:22pm    
What error does it result in?
BernardIE5317 3-Dec-22 16:26pm    
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.

The problem is probably you didn't enclose paths in quotes. For example, look closely:
system("C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe" ...");

should be
system("\"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe\" ...");

and so on for all the command line parameters where you have to enclose arguments in quotes. The \" escape sequence puts a quote in the string literal.
 
Share this answer
 
v2
Comments
BernardIE5317 2-Dec-22 20:34pm    
Thank You for Your Kind assistance. Please see update to question as it now presents how the argument was/is passed to system() which I believe includes double quotes around the path in the argument.
Dave Kreskowiak 2-Dec-22 20:38pm    
The problem is when you said "The same identical command run from the command line...", the command in your code is NOT the same as the command line at the prompt. You do NOT have quotes inside the string in the code, like I spelled out in my answer.

You "believe" you have the quotes in the string correctly, but you don't. The quotes you see in the code are the quotes that denote a string literal. Your string literal does NOT have the appropriate quotes in it. This is why you're getting the errors.

string cl_path = "C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe";

Should be:
string cl_path = "\"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe\"";
BernardIE5317 2-Dec-22 21:02pm    
I did as you suggested i.e. cl_path = "\"C:Program ...CL.exe\"" The result is the same as I expected i.e. CMD reported 'C:/Program' is not recognized as an internal or external command,` as I believe adding the term sdbl_quotes to the original form of cl_path served the same purpose.
Dave Kreskowiak 2-Dec-22 21:06pm    
If you're getting the same error, that means the escaped quotes are not in the string your passing to the system call.

By adding all the sspace, sbdl_quotes, ..., you are making this needlessly complex. Just pass the entire escaped string to system and make sure your complete command line works.
system(cl_path + " " + cl_options);
BernardIE5317 3-Dec-22 16:16pm    
Please see improved question which demonstrates source of error is somehow due to 3rd optional argument.
This is more or less the same issue as you had yesterday. Paths with backslashes or spaces need to be treated specially to prevent the command processor from misinterpreting them. you should change the first line to:
C++
string cl_path = "\"C:/Program Files/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.35.32019/bin/HostX64/x64/CL.exe\"";

That way, the system call will pass a quoted string to the command processor, which will see it as a complete path.
 
Share this answer
 
Comments
BernardIE5317 3-Dec-22 16:16pm    
Please see improved question which demonstrates source of error is somehow due to 3rd optional argument.
Richard MacCutchan 4-Dec-22 3:01am    
Which is what I explained in the previous question.
BernardIE5317 4-Dec-22 4:29am    
Yes I recall and am most appreciative of. However in this case as explained in the improved question the specific cause of the error is not the path separator character as was the case in the previous question as you will note and plainly see the troublesome path separator character is not utilized anywhere in this question instead the path separator character you kindly discovered is but rather the presence of the double quotes characters in the option argument is the root of all evil here. Upon removal of said double quotes characters from option arguments as shown in improved question the error disappeared and cl.exe was successfully called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900