我正在使用 CreateProcess
调用 cl
将另一个 C++ 程序 ( TestProg.cxx
) 编译并链接到 DLL 中。我调用 cl.exe
具有以下编译选项:
编译选项:
/Od /nologo /Fo /RTC /w /Zc TestProg.cxx /DLL
电话:
if ( CreateProcess(PATH_TO_EXE, COMPILATION_OPTIONS, NULL,NULL,
FALSE,0,NULL,NULL,&si,&pi) )
{
//....
}
如果TestProg.exe
包含 #include <iostream.h>
我收到以下编译错误:
TestProg.cpp(1) : fatal error C1034: iostream.h: no include path set
没有任何#include
命令,我收到以下链接错误:
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
我做错了什么?
我在网上搜索了最近 6-7 个小时的答案,但没有找到。使用 Windows API 对我来说是新的。
最佳答案
如果您查看 CreateProcess
的定义:
BOOL WINAPI CreateProcess(
__in_opt LPCTSTR lpApplicationName,
__inout_opt LPTSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
您正在设置可选参数 __in_opt LPVOID lpEnvironment,
至 NULL
.
根据上述定义:
A pointer to the environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.
cl.exe
从环境变量中获取其包含位置信息和库搜索路径 - 查看 setenv.bat
在 VS 目录中。在这种情况下,您的调用进程或目标进程都没有在设置了这些变量的环境中运行。
你有一个选择——你可以根据MSDN自己创建环境变量:
An environment block consists of a null-terminated block of null-terminated strings. Each string is in the following form:
name=value\0
Because the equal sign is used as a separator, it must not be used in the name of an environment variable.
或者您可以要求您的程序在 VS 工具提示符下运行。一个很好的检查,这实际上是问题所在,从这个提示符而不是 Visual Studio 运行你的程序,看看是否能解决问题。
不使用 #include
的原因产生链接器错误是因为如果没有包含,cl.exe
不会查找它们 - 然后它会查找 C/C++ 运行时库。
附带说明 - 我相信 C++ 中的标准是 #include <iostream>
即没有 .h
.
关于c++ - 如何设置从 CreateProcess 创建的进程的搜索路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710893/