开发者

Boost c++ static link paths under cygwin

开发者 https://www.devze.com 2023-03-03 12:37 出处:网络
I wrote a program in c++ leveraging header files from boost. When I compiled it I noticed I had some undefined symbols for boost system and boost file system (which I expected). I\'ve finally gotten m

I wrote a program in c++ leveraging header files from boost. When I compiled it I noticed I had some undefined symbols for boost system and boost file system (which I expected). I've finally gotten my program compile and link, but I arguably don't understand why. If someone could lend some insight on the following I would appreciated it.

The final compile statement is: g++ dcc_to_png.c -lboost_system-mt -lboost_filesystem-mt but looking at boost/state/lib the library names listed under /cygdrive/c/Users/Joe/My\ Documents/My\ Dropbox/Code/boost_1_46_1/stage/lib are libboost_system.a and libboost_filesystem.a.

What I don't understand is

a) What are these strings I'm using boost_system-mt etc? Where are they defined what do they refer to? How do they resolve to actual libs?

b) For some reason if I try to do this on my own, ld complains it can't find the lib. For example:

g++ dcc_to_png.c -L"/cygdrive/c/Users/Joe/My Documents/My D开发者_如何转开发ropbox/Code/boost_1_46_1/stage/lib" -llibboost_system.a -llibboost_filesystem.a

yields:

/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -llibboost_system.a /usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -llibboost_filesystem.a collect2: ld returned 1 exit status

c) In the statement that compiles + links, why do I have to use -mt at the end of boost_system? I understand what -mt denotes, but how does that resolve to a lib on my filesystem?


Okay - so found some stuff. First in terms of search paths see: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html. This helps clarify search paths when you link.

Secondly - note the idea of using -mt at the end of the name is from an older boost convention which is no longer necessary.

See: http://groups.google.com/group/linux.debian.bugs.dist/browse_thread/thread/5177d8bf13791038?pli=1

Last but not least - if you are a cygwin user like me, there are a few gotchas you should be aware of. When you install the devel tools you may have installed version of boost headers prior to 46_1 (which I am using). These will be in /usr/include/boost/ and if you use these headers after you build boost on your system your libs won't match the headers you find and you will always get a undefined reference to boost::system::get_system_category() because these headers may expect you to reference the-mtlibs. If you didn't build boost on your box this will work fine if you just include a-mt` at the end of the lib in question (it did for me on another box, which was mind boggling until I figured it out).

The answer to this for me was to back up (just in case) and then wipe out the headers in /usr/include/boost/ and then force include my source code's boost headers ala -I and force link the full paths to my libs. This forces g++ to use the libs + the headers that match.

$ g++ dcc_to_png.c dccinfo.c -I"$CODEDIR/boost_1_46_1/" -include dccinfo.h /usr/lib/filesystem/build/gcc-4.3.4/release/link-static/threading-multi/libboost_filesystem.a /usr/lib/system/build/gcc-4.3.4/release/link-static/threading-multi/libboost_system.a -o dcc_to_png.exe

This works - but it will only work if g++ isn't finding those old headers. If you haven't built boost and you want this to work - I believe you can leave out the -I and just use -lboost_filesystem-mt -lboost_system-mt but this ABSOLUTELY depends on the VERSION of boost. (see the thread above).


Not

-llibboost_system.a -llibboost_filesystem.a

But

-lboost_system -lboost_filesystem

Or provide full path

g++ dcc_to_png.c /cygdrive/c/Users/Joe/My Documents/My Dropbox/Code/boost_1_46_1/stage/lib/libboost_filesyste.a /cygdrive/c/Users/Joe/My Documents/My Dropbox/Code/boost_1_46_1/stage/lib/libboost_system.a
0

精彩评论

暂无评论...
验证码 换一张
取 消