简体   繁体   中英

How to run in debug mode a program that calls link with CreateProcess

I am using CreateProcess to invoke cl and link to compile and link another C++ program ( TestProg.cxx ) into a DLL. I found the correct compilation and linkage options:

Compilation Options:

/W3 /WX- /Od /Oy- /D WIN32 /D _DEBUG /D _WINDOWS /D _USRDLL /D BUILDDLL_EXPORTS /D _WINDLL /D _UNICODE /D UNICODE /GS /fp:precise /Zc:wchar_t /Zc:forScope /Gd /analyze- /errorReport:queue /LD 

Linkage Options:

/INCREMENTAL /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /ERRORREPORT:QUEUE /Entry:DllMain 

I call CreateProcess with:

if ( CreateProcess(full path to cl.exe, compilation options, NULL,NULL,FALSE,0,       NULL,NULL,&si,&pi) ) 
{
 //....
}

Running the application from VS tools prompt, it works and the dll is created.

But running it from the VS debugger I get the following LINK error:

LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

or when the link libs are removed from the link options I get the following error:

LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'

What am I doing wrong?

IMO it seems something is missing in the link options to run in debug mode, or the search path of debug mode lacks some directories. I don't know how to fix any of these cases. I googled this for the last 1/2 day, but didn't find it. Using windows API is new to me.

Many thanks in advance

You need to specify the location of the .lib files. Otherwise link does not know where to find your .lib files. You can do this either as command line arguments to link ( /LIBPATH:dir ) or by setting the LIB environment variable.

Exactly where these .lib files are to be found depends on your installation. Open a Visual Studio command prompt and type set LIB to find out what is an appropriate setting for your installation.

Myself I'd be trying to avoid having to use CreateProcess for build automation since it has a pretty painful interface. I'd be looking for a higher level scripting language. I would also prefer to use vcbuild rather than calling cl and link by hand. But perhaps there is some good reason why you need to do this from C++ code that I am not aware of.

Sounds like you're missing the environment variables which you get when running from the VS command line. Part of this is also the library search path.

See C:\\Program Files\\Microsoft Visual Studio XX\\Common7\\vsvars32.bat (depending on your VS version and installation path, which is specified in the VS100COMNTOOLS (or VS90COMNTOOLS , or which ever) environment variable.

Why are you doing this anyway? You could be using MSBuild to configure a project and build it. You can call MSBuild from your code, while the project is already configured for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM