如何在多个类中使用库而无需获得“多重定义”错误
我正在开始一个新的 C++ 项目,我想使用 Boost.Build / bjam。
我收到“多重定义”错误,因为我认为可能是 jam 文件编写不正确,或者我没有正确包含标题,或者,也许库编写得不好,我还没有调查过。
main.cpp 使用 libA。 rest.cpp 使用 libA。
libA 是一个头库。因此,我需要在编译器抱怨命名空间等的 main.cpp 和 rest.cpp 中包含 libA 的标头...
我在 debian 上使用 gcc。
这是我的果酱文件:
Jamroot.jam
import os ;
import modules ;
path-constant boost-root : "/home/dude/include/boost_1_48_0" ;
path-constant cgi-root : "/home/dude/include/cgi-0.7.1/libs/cgi/build" ;
path-constant BOOST_BUILD_PATH : "$(boost-root)/tools/build/v2" ;
# path-constant include-dir : /usr/local/include ;
use-project /boost/ : $(boost-root) ;
use-project /boost/cgi/ : $(cgi-root) ;
lib libsoci_core : : <file>/usr/local/lib/libsoci_core.so ;
lib libsoci_odbc : : <file>/usr/local/lib/libsoci_odbc.so ;
lib libboost_log : : <file>/usr/local/lib/libboost_log.so ;
Jamfile.jam
project hello_fcgi
: requirements
<library>/boost/cgi/
<library>/boost/system/
<library>/boost/thread/
<include>/usr/local/include/soci/
<include>/usr/local/include/soci/odbc/
;
# exe rest : rest.cpp hello /boost/regex/ libboost_log libsoci_core libsoci_odbc ;
exe hello : main.cpp rest.cpp cms.cpp /boost/regex/ libboost_log libsoci_core libsoci_odbc ;
# Our install rule (builds binaries and copies them to <location>)
install install
:
hello
:
<location>/var/www/localhost/cgi-bin/
;
# Only install example if you use `bjam install' or equivalent
explicit install ;
我得到的错误是重复的:
Performing configuration checks
- has_icu builds : yes
...patience...
...patience...
...found 3228 targets...
...updating 1 target...
gcc.link bin/gcc-4.4.5/debug/hello
bin/gcc-4.4.5/debug/rest.o: In function `basic_client':
/usr/include/c++/4.4/exception:62: multiple definition of `boost::cgi::common::basic_client<boost::cgi::common::tags::fcgi>::basic_client()'
bin/gcc-4.4.5/debug/main.o:/home/dude/include/cgi-0.7.1/boost/cgi/fcgi/client.hpp:44: first defined here
I am starting a new c++ project and I want to use Boost.Build / bjam.
I'm getting "multiple definition" errors because, I think maybe, the jam file is not written correctly or I am not including the headers correctly or, perhaps, the library is not written well, which I have not investigated.
main.cpp uses libA.
rest.cpp uses libA.
libA is a header library. Therefore I need to include headers for libA in both main.cpp and rest.cpp of the compiler complains about namespaces etc...
I'm using gcc on debian.
Here are my jam files:
Jamroot.jam
import os ;
import modules ;
path-constant boost-root : "/home/dude/include/boost_1_48_0" ;
path-constant cgi-root : "/home/dude/include/cgi-0.7.1/libs/cgi/build" ;
path-constant BOOST_BUILD_PATH : "$(boost-root)/tools/build/v2" ;
# path-constant include-dir : /usr/local/include ;
use-project /boost/ : $(boost-root) ;
use-project /boost/cgi/ : $(cgi-root) ;
lib libsoci_core : : <file>/usr/local/lib/libsoci_core.so ;
lib libsoci_odbc : : <file>/usr/local/lib/libsoci_odbc.so ;
lib libboost_log : : <file>/usr/local/lib/libboost_log.so ;
Jamfile.jam
project hello_fcgi
: requirements
<library>/boost/cgi/
<library>/boost/system/
<library>/boost/thread/
<include>/usr/local/include/soci/
<include>/usr/local/include/soci/odbc/
;
# exe rest : rest.cpp hello /boost/regex/ libboost_log libsoci_core libsoci_odbc ;
exe hello : main.cpp rest.cpp cms.cpp /boost/regex/ libboost_log libsoci_core libsoci_odbc ;
# Our install rule (builds binaries and copies them to <location>)
install install
:
hello
:
<location>/var/www/localhost/cgi-bin/
;
# Only install example if you use `bjam install' or equivalent
explicit install ;
The errors I get are repetitions of:
Performing configuration checks
- has_icu builds : yes
...patience...
...patience...
...found 3228 targets...
...updating 1 target...
gcc.link bin/gcc-4.4.5/debug/hello
bin/gcc-4.4.5/debug/rest.o: In function `basic_client':
/usr/include/c++/4.4/exception:62: multiple definition of `boost::cgi::common::basic_client<boost::cgi::common::tags::fcgi>::basic_client()'
bin/gcc-4.4.5/debug/main.o:/home/dude/include/cgi-0.7.1/boost/cgi/fcgi/client.hpp:44: first defined here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个缺陷报告看起来很相关:似乎这个“仅标头”库已经滑倒了有些东西不仅仅是标题。
This defect report looks relevant: it seems this "header only" library has slipped up on a few things which aren't really just header only.