Jump to content
• Advertisement

# linking programmatically on msvc

This topic is 3292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi folks I want to be able to link to library files that reside on C:\sdk\lib such as: - C:\sdk\lib\foo.lib, C:\sdk\lib\bar.lib etc.. This is easily achieved in MSVC by filling up the "C:\sdk\lib" in project's setting for Debug|Release(within the linking section). However, is there any way to do this programmatically using various directives (#ifdefs, #defines)? Or, even a different way(although programmatically)? Thanks tons! :-)

#### Share this post

##### Share on other sites
Advertisement
This is widely considered to be a bad idea. What if the person who wants to build the source code isn't you and has the libraries in the wrong place? What if it IS you but on a new windows version that has the libraries in a new place, or on a new development environment or a fresh install, etc. There is a reason why that is supposed to be so easy to configure. Additionally problematic is, what if someone wants to build your code using a different compiler, or what if the link library changes names on different systems (macOS X linking to openGL is done by specifying -lGL, which is linking to libGL.so.*). Lastly, programmatic linking overrides the link settings you specify, which can cause weird issues, like for example, static libraries versus dynamic ones. The last time I tried to build freeglut, I wanted a purely static library, and I built it...but because freeglut.h does implicit automatic linking to the dynamic library version of freeglut, everywhere I used #include <freeglut.h> in an executable made it so that my executable required freeglut.dll to run, even though I had explicitly told it NOT to do that. It took a few hours of tracing the source code and the freeglut manual to figure out what was going on, and it really pissed me off. Obviously, your mileage may vary, though, so you can do what you like.

If you choose to ignore all of the above, or have considered it and decided you don't care, however, the way to do what you are asking is

#pragma comment(lib,"libraryfile")

which is used like

#pragma comment(lib,"freeglut.lib")

more details at http://msdn.microsoft.com/en-us/library/7f0aews7(VS.80).aspx

#### Share this post

##### Share on other sites
Steve132
Hi. Yep I do understand the consequence of using this on a "real" project. I asked because it was ridiculously boring to create projects mundanely for various examples I am working on to teach myself about Boost. Boost::any is superb! :-) So is the asynch io. wow! :-)

Rest assured, I will never ever using this on production code. :-)

#### Share this post

##### Share on other sites
if its dynamic libraries you could use the loadlibrary method from the win32 api, it might work on static libraries too but i have never tried. also have to know the name of the methods or objects in the file.

#### Share this post

##### Share on other sites
Below does the job perfectly(for a code doing asynch io):

//debug#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_system-vc90-mt-gd-1_40.lib")#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_date_time-vc90-mt-gd-1_40.lib")#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_regex-vc90-mt-gd-1_40.lib")//release#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_system-vc90-mt-1_40.lib")#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_date_time-vc90-mt-1_40.lib")#pragma comment(lib,	"C:\\Boost\\boost_1_40_0\\stage\\lib\\libboost_regex-vc90-mt-1_40.lib")

thanks Steve132 and owiley :-)
owiley, with regards to the ::LoadLibrary(...), I moved on because absolute lib paths weren't being loaded. I am more than happy with #pragma directive :-) Thanks for the pointer though.

As previously noted, I will never use #pragma on production code. Thanks Steve132 :-)

#### Share this post

##### Share on other sites
Incidentally, since it is boost you are trying to get working, I should inform you that boost autolinks in the header-file already, and you shouldn't need to do this at all unless your boost installation is fubar. Try making a project that uses the boost libs you want only with incldue directives, and only set the link directory to the boost installation lib directory, but do not set any specific libs, and see if the header autolinking code works correctly.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5
• Advertisement

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632563
• Total Posts
3007099

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!