Good techniques to use Makefiles in VisualStudio?

2019-02-14 17:33发布

I know the ideal way to build projects is without requiring IDE based project files, since it theoretically causes all sort of trouble with automation and what not. But I've yet to work on a project that compiles on Windows that doesn't depend on the VisualStudio project (Ok, obviously some Open Source stuff gets done with Cygwin, but I'm being general here).

On the other hand if we just use VS to run a makefile, we loose all the benefits of the compile options window, and it becomes a pain to maintain the external makefile.

So how do people that use VS actually handle external makefiles? I have yet to find a painless system to do this...

Or in reality most people don't do this, although its preached as good practice?

11条回答
时光不老,我们不散
2楼-- · 2019-02-14 18:16

Personally, I use Rake to call msbuild on my solution or project. For regular development I use the IDE and all the benefits that provides.

Rake is set up so that I can just compile, compile and run tests or compile run tests and create deployable artifacts.

Once you have a build script it is really easy to start doing things like setting up continuous integration and using it to automate deployment.

You can also use most build tools from within the IDE if you follow these steps to set it up.

查看更多
仙女界的扛把子
3楼-- · 2019-02-14 18:16

Why would you want to have project that "compiles on Windows that doesn't depend on the VisualStudio project"? You already have a solution file - you can just use it with console build.

I'd advise you to use msbuild with conjunction with makefile, nant or even simple batch file if your build system is not as convoluted as ours...

Is there something I'm missing?

查看更多
倾城 Initia
4楼-- · 2019-02-14 18:20

Take a look at MSBuild!

  • MSBuild can work with the sln/csproj files from VS, so for simple projects you can just call them directly.
  • if you need more control, wrap the projects in your own build process, add your own tasks etc. - it is very extensible!

(I wanted to add a sample but this edior totally messed up the XML... sorry)

查看更多
手持菜刀,她持情操
5楼-- · 2019-02-14 18:21

We use the devenv.exe (same exe that launches the IDE) to build our projects from the build scripts (or the command line). When specifying the /Build option the IDE is not displayed and everything is written back to the console (or the log file if you specify the /Out option)

See http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx for more information

Example:

devenv.exe [solution-file-name] /Build [project-name] /Rebuild "Release|Win32" /Out solution.log

where "Release|Win32" is the configuration as defined in the solution and solution.log is the file that gets the compiler output (which is quite handy when you need to figure out what went wrong in the compile)

查看更多
聊天终结者
6楼-- · 2019-02-14 18:25

How about this code?

public TRunner CleanOutput()
{
    ScriptExecutionEnvironment.LogTaskStarted("Cleaning solution outputs");

    solution.ForEachProject(
        delegate (VSProjectInfo projectInfo)
            {             
                string projectOutputPath = GetProjectOutputPath(projectInfo.ProjectName);

                if (projectOutputPath == null)
                    return;

                projectOutputPath = Path.Combine(projectInfo.ProjectDirectoryPath, projectOutputPath);

                DeleteDirectory(projectOutputPath, false);

                string projectObjPath = String.Format(
                    CultureInfo.InvariantCulture,
                    @"{0}\obj\{1}",
                    projectInfo.ProjectName,
                    buildConfiguration);
                projectObjPath = Path.Combine(productRootDir, projectObjPath);
                DeleteDirectory(projectObjPath, false);
            });

    ScriptExecutionEnvironment.LogTaskFinished();
    return ReturnThisTRunner();
}

public TRunner CompileSolution()
{
    ScriptExecutionEnvironment.LogTaskStarted ("Compiling the solution");

    ProgramRunner
        .AddArgument(MakePathFromRootDir(productId) + ".sln")
        .AddArgument("/p:Configuration={0}", buildConfiguration)
        .AddArgument("/p:Platform=Any CPU")
        .AddArgument("/consoleloggerparameters:NoSummary")
        .Run(@"C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe");

    ScriptExecutionEnvironment.LogTaskFinished ();
    return ReturnThisTRunner ();
}

You can find the rest of it here: http://code.google.com/p/projectpilot/source/browse/trunk/Flubu/Builds/BuildRunner.cs

查看更多
登录 后发表回答