简体   繁体   中英

How to make Visual Studio 2017 C++ project more portable between computers?

I am developing a project on C++ which relies on many of third-party libraries (*.lib files and *.h files). I store these libraries in a folder which is not dependant to project, like C:/thirdpartylib. Relative paths is not an option, since it becomes way too long. I have defined connections to libraries in linker setting and in general C++ settings.

But when I pass the project to supervisor he has to reset all paths to libraries to match his environment. We use git, and the project file is being tracked. He stores thirdparty libraries in another way than me.

Is there any way to make a project more portable? Maybe it is possible to store paths in some sort of config files?

The way to go for large project is to use a package manager. There are some good options out there. Perhaps in windows and visual studio you can use vcpkg or NuGet unmanaged.

If you cannot use a package manager for some reason, the next thing to do is to commit all the dependencies to the GIT repo. If you only target windows platforms like windows 8 or 10 and want to support only VS2017 then committing the compiled dependencies is not a problem. The downside is that the repo will become huge.

For a tiny school project the latter option is viable.

As @gaurav says, the way to deal with this in Visual Studio is with property sheets . It's unfortunate that this term is used for two different things in VS, but I guess they just ran out of names (spoiler alert).

These are very powerful, once you learn how they work, and they're just what you need here because they let you define macros , and these macros can in turn be used in the rest of your project to refer to the (volatile) location of your various libraries. This is a trick that everyone who uses VS should know, but it seems that a lot of people don't.

I don't think it's worth me trying to walk you through the mechanics of setting one up here because Microsoft already document it in the Visual Studio help file. Suffice to say, you do it in the Property Manager , that should help you track down the relevant information.

There is also an excellent blog post here which I recommend you read before you do anything else:

http://www.dorodnic.com/blog/2014/03/20/visual-studio-macros/

It's also on Wayback Machine here:

https://web.archive.org/web/20171203113027/http://www.dorodnic.com/blog/2014/03/20/visual-studio-macros/

OK, so now we know how to define a macro, what can we do with it?

Well, that's actually the easy part. If we have a macro called, say, FOO , then wherever we want to expand that macro in some project setting or other we can just use $(FOO) . There's also a bunch of macros built into the IDE as listed here:

https://msdn.microsoft.com/en-us/library/c02as0cs.aspx

So, you, I imagine, will want to define macros for the include and lib directories for each of your external libraries and you can then use these to replace the hard-coded paths you are currently using in your project.

And that, I reckon, should sort you out, because the definitions of the macros themselves are stored in a separate file , external to your project file, and different users / build machines can use different files. IIRC, these have extension .props .

Also, you can define a macro in terms of another macro or macros, and that makes the job easier still.

So, who still thinks that Microsoft don't know how to create a build system? Visual Studio is a fantastic piece of software once you get used to it, there's just a bit of a learning curve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM