gpt4 book ai didi

c++ - 以编程方式修改/创建 vcproj 文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:05 24 4
gpt4 key购买 nike

我有一个 visual c++ 项目文件 (vcproj),我想以编程方式修改它以添加额外的包含目录或链接库。

一种解决方案是将 vcproj 解析为 XML 文件并对其进行修改。有任何其他 API 可以使这更容易吗?

最佳答案

这是我编写的一些代码,用于从 Visual Studio 解决方案 (.sln) 中获取它包含的所有项目,以及从每个项目中获取它包含的所有文件(包括完整路径)。

static strList parseSolution( const char * solName  )
{
strList result;
static char drive[_MAX_DRIVE];
static char somepath[_MAX_PATH];
static char buffer[_MAX_PATH];
static char path[_MAX_PATH];
static char ext[_MAX_EXT];

_splitpath( solName, drive, somepath, buffer, ext );

FILE * f = fopen( solName, "r" );
if( NULL == f )
{
printf("ERROR: Solution %s is missing or unavailable.\n", solName );
exit(1);
}

while( !feof(f) )
{
char * res = fgets( buffer, sizeof(buffer), f );
if( NULL == res )
continue;

if( NULL != strstr(buffer, "Project(") )
{
char * ptrName = strchr( buffer, '=' );
char * ptrFile = strchr( ptrName, ',' );
*ptrFile++ = 0;
char * ptrEnd = strchr( ptrFile, ',' );
*ptrEnd++ = 0;

while( ('=' == *ptrName)
||(' ' == *ptrName)
||('"' == *ptrName) ) ptrName++;
if( '"' == ptrName[strlen(ptrName)-1] )
ptrName[strlen(ptrName)-1] = 0;

while( (' ' == *ptrFile)
||('"' == *ptrFile) ) ptrFile++;
if( '"' == ptrFile[strlen(ptrFile)-1] )
ptrFile[strlen(ptrFile)-1] = 0;

_makepath( path, drive, somepath, ptrFile, NULL );

result.push_back( std::string(path) );
}
}

fclose(f);

return result;
}

/**
* Parse project and extract fullpath source filename from project.
*/
static strList parseProject( const char * projName )
{
strList result;
static char drive[_MAX_DRIVE];
static char somepath[_MAX_PATH];
static char buffer[_MAX_PATH];
static char path[_MAX_PATH];
static char ext[_MAX_EXT];

_splitpath( projName, drive, somepath, buffer, ext );

FILE * f = fopen( projName, "r" );
if( NULL == f )
{
printf("ERROR: Project %s is missing or unavailable.\n", projName );
exit(1);
}

while( !feof(f) )
{
char * res = fgets( buffer, sizeof(buffer), f );
if( NULL == res )
continue;

if( (NULL != strstr(buffer, "<ClInclude Include="))
||(NULL != strstr(buffer, "<ClCompile Include=")) )
{
char * ptrName = strchr( buffer, '=' );
char * ptrName1 = strstr( buffer, "/>" );
if( NULL != ptrName1 ) *ptrName1 = 0;

while( ('=' == *ptrName)
||(' ' == *ptrName)
||('"' == *ptrName) ) ptrName++;
while( ('"' == ptrName[strlen(ptrName)-1])
||(' ' == ptrName[strlen(ptrName)-1])
||('\n' == ptrName[strlen(ptrName)-1]))
ptrName[strlen(ptrName)-1] = 0;

_makepath( path, drive, somepath, ptrName, NULL );
result.push_back( std::string(path) );
}
}

fclose(f);

return result;
}

/**
* Recoding source file.
*/

使用这些功能,您可以处理每个文件,或处理项目本身。

  strList projectList = parseSolution( argv[1] );
strList::iterator itProj = projectList.begin();
while( itProj != projectList.end() )
{
printf("Project: %s\n", itProj->c_str());
strList fileName = parseProject( itProj->c_str() );
strList::iterator itFile = fileName.begin();
while( itFile != fileName.end() )
{
printf(" File %s\n", itFile->c_str());
// do something with the project file
itFile++;
}
fileName.clear();
itProj++;
}

关于c++ - 以编程方式修改/创建 vcproj 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com