Visual studio 2015.如何在编译时在代码中获取项目名称

Visual studio 2015. How get project name in code at compile time?

本文关键字:获取 项目 代码 编译 2015 studio Visual      更新时间:2023-10-16

我可以使用宏__FILE__获得文件的名称吗?如何获得项目名称?

https://msdn.microsoft.com/library/b0084kay.aspx

一个简单的例子:

#include "stdafx.h"
#include <iostream>
#include <string>
#if !defined PROJECT_NAME
#define PROJECT_NAME ""
#endif
int _tmain(int argc, _TCHAR* argv[])
{
    std::string str(PROJECT_NAME);
    std::cout << str;
    return 0;
}

在项目属性的预处理器定义选项中,在已经存在的定义的末尾添加以下内容:

PROJECT_NAME = " $ (ProjectName) ";

只需在项目设置中添加编译器定义。项目名称显然是项目范围的,所以应该很容易。

您也可以使用$(ProjectName)构建系统宏(可用的宏可以在编辑预处理器定义时看到使用<Edit..>在下拉框中的值),并将定义放在您包含在所有项目中的属性表中。

在c++中argv[0]是可执行文件名。通常,如果您使用默认项目设置,则将可执行名称设置为项目名称。

 int main(int argc, char **argv)
 {
    std::cout<<"Application name :"<< argv[0]<<endl;
    return 0;
}