Create a new visual studio project, add files and run them - command line

Is there a way to create a new visual studio project without using an IDE, use the command line instead

I am working on a project that will generate a small C ++ program, so I want to create a new project, add this C ++ file to this project, compile and run it using only the command line (batch file) ..

so can anyone please let me know how to do this .. thanks in advance.

+4
source share
2 answers

Let CMake create the project files for you. It uses a much clearer syntax, and you can also generate project files for many other build systems.

As an example, here is a very simple CMake file:

project(Foo) cmake_minimum_required(VERSION 2.8) #project source files file (GLOB HEADER_FILES "*.h" "*.hpp") file (GLOB SOURCE_FILES "*.cpp") # build add_executable(Foo ${SOURCE_FILES}) target_link_libraries(Foo ${LIBS}) 

And here is the solution file that it generates.

 Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ALL_BUILD", "ALL_BUILD.vcxproj", "{66E5A2EB-A802-44A1-AC9C-906752330405}" ProjectSection(ProjectDependencies) = postProject {1A246EDB-1F39-4776-A9C0-C81AC67D1924} = {1A246EDB-1F39-4776-A9C0-C81AC67D1924} {A0601C1A-BC0F-45D0-BDB1-C5056BD69958} = {A0601C1A-BC0F-45D0-BDB1-C5056BD69958} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Foo", "Foo.vcxproj", "{1A246EDB-1F39-4776-A9C0-C81AC67D1924}" ProjectSection(ProjectDependencies) = postProject {A0601C1A-BC0F-45D0-BDB1-C5056BD69958} = {A0601C1A-BC0F-45D0-BDB1-C5056BD69958} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZERO_CHECK", "ZERO_CHECK.vcxproj", "{A0601C1A-BC0F-45D0-BDB1-C5056BD69958}" ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Release|Win32 = Release|Win32 MinSizeRel|Win32 = MinSizeRel|Win32 RelWithDebInfo|Win32 = RelWithDebInfo|Win32 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {66E5A2EB-A802-44A1-AC9C-906752330405}.Debug|Win32.ActiveCfg = Debug|Win32 {66E5A2EB-A802-44A1-AC9C-906752330405}.Release|Win32.ActiveCfg = Release|Win32 {66E5A2EB-A802-44A1-AC9C-906752330405}.MinSizeRel|Win32.ActiveCfg = MinSizeRel|Win32 {66E5A2EB-A802-44A1-AC9C-906752330405}.RelWithDebInfo|Win32.ActiveCfg = RelWithDebInfo|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.Debug|Win32.ActiveCfg = Debug|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.Debug|Win32.Build.0 = Debug|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.Release|Win32.ActiveCfg = Release|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.Release|Win32.Build.0 = Release|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.MinSizeRel|Win32.ActiveCfg = MinSizeRel|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.MinSizeRel|Win32.Build.0 = MinSizeRel|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.RelWithDebInfo|Win32.ActiveCfg = RelWithDebInfo|Win32 {1A246EDB-1F39-4776-A9C0-C81AC67D1924}.RelWithDebInfo|Win32.Build.0 = RelWithDebInfo|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.Debug|Win32.ActiveCfg = Debug|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.Debug|Win32.Build.0 = Debug|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.Release|Win32.ActiveCfg = Release|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.Release|Win32.Build.0 = Release|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.MinSizeRel|Win32.ActiveCfg = MinSizeRel|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.MinSizeRel|Win32.Build.0 = MinSizeRel|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.RelWithDebInfo|Win32.ActiveCfg = RelWithDebInfo|Win32 {A0601C1A-BC0F-45D0-BDB1-C5056BD69958}.RelWithDebInfo|Win32.Build.0 = RelWithDebInfo|Win32 EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EndGlobalSection GlobalSection(ExtensibilityAddIns) = postSolution EndGlobalSection EndGlobal 

The project file is similarly ugly and 294 lines long.

Adding dependencies is pretty simple, here's how you add boost:

 find_package(Boost REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) set(LIBS ${LIBS} ${Boost_LIBRARIES}) 
+2
source

Visual Studio projects are just XML files, so you can simply examine their format and write them. (The format has changed from 2008 to 2010). Decision files are a custom text format, but not too complex.

Finally, devenv.exe has a switch for "do not run the IDE, just compile this solution on the command line", which you can use to compile the resulting solution.

+4
source

All Articles