我用 c++ 编写了一个程序,利用 boost 的头文件。当我编译它时,我注意到我有一些用于 boost 系统和 boost 文件系统的未定义符号(这是我所期望的)。我终于编译并链接了我的程序,但我可能不明白为什么。如果有人可以对以下内容提供一些见解,我将不胜感激。
最终编译语句为: g++ dcc_to_png.c -lboost_system-mt -lboost_filesystem-mt
但查看 boost/state/lib /cygdrive/c/Users/Joe/My\ Documents/My\ Dropbox/Code/boost_1_46_1/stage/lib 下列出的库名称是 libboost_system.a 和 libboost_filesystem.a。
我不明白的是
a) 我使用 boost_system-mt 等的这些字符串是什么?它们在哪里定义,它们指的是什么?他们如何解析为实际的库?
b) 由于某种原因,如果我尝试自己执行此操作,ld 会抱怨它找不到该库。例如:
g++ dcc_to_png.c -L"/cygdrive/c/Users/Joe/My Documents/My Dropbox/Code/boost_1_46_1/stage/lib" -llibboost_system.a -llibboost_filesystem.a
产量:
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld:找不到-llibboost_system.a
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld:找不到-llibboost_filesystem.a
collect2:ld返回1退出状态
c) 在编译+链接的语句中,为什么我必须在boost_system末尾使用-mt?我理解 -mt 表示什么,但是它如何解析为我的文件系统上的库?
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 Dropbox/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?
发布评论
评论(2)
好的 - 所以找到了一些东西。首先就搜索路径而言,请参阅: http://gcc.gnu.org/onlinedocs /gcc/Link-Options.html。这有助于在链接时澄清搜索路径。
其次 - 请注意在名称末尾使用 -mt 的想法来自旧的 boost 约定,不再需要。
请参阅:http://groups.google .com/group/linux.debian.bugs.dist/browse_thread/thread/5177d8bf13791038?pli=1
最后但并非最不重要的 - 如果您是像我一样的 cygwin 用户,那么您应该注意一些问题。当您安装开发工具时,您可能已经安装了 46_1 之前的 boost 标头版本(我正在使用)。这些将位于 /usr/include/boost/ 中,如果您在系统上构建 boost 后使用这些标头,您的库将与您找到的标头不匹配,并且您将始终获得对 boost::system 的未定义引用::get_system_category() 因为这些标头可能期望您引用-mt
库。如果你没有在你的盒子上构建 boost,那么只要在相关库的末尾包含一个
-mt` 就可以正常工作(它在另一个盒子上为我做了,这令人难以置信,直到我发现出来)。对我来说,这个问题的答案是备份(以防万一),然后清除
/usr/include/boost/
中的标头,然后强制包含我的源代码的 boost 标头 ala-我
并强制将完整路径链接到我的库。这会强制 g++ 使用匹配的库 + 标头。这是有效的 - 但只有当 g++ 没有找到那些旧的标头时它才会有效。如果您还没有构建 boost 并且您希望它起作用 - 我相信您可以省略
-I
并仅使用-lboost_filesystem-mt -lboost_system-mt
但这绝对取决于 boost 的版本。 (参见上面的线程)。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.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
But
Or provide full path