Sign in to follow this  
setiz1

linking programmatically on msvc

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


Link to post
Share on other sites
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this