【问题标题】:c++ header multiple usagec++ header 多次使用
【发布时间】:2013-09-25 02:09:27
【问题描述】:

我有一个模板类 A 和一个类型参数 T。 我的程序为类型参数生成了几个版本 - T_1、..、T_N。此外,对于每个类型T_i,带有A<T_i> 的代码被编译到库lib_i 中。最后,对于每个lib_i,我从A<T_i> 调用一个函数。但是,我总是得到与T_1 对应的结果。可能是什么问题?

这里有更多细节。

文件结构:

  • A.hpp
  • lib1
    • main1.cpp
    • T1.hpp
    • lib1.so
  • lib2
    • main2.cpp
    • T2.hpp
    • lib2.so
  • libN
    • mainN.cpp
    • TN.hpp
    • libN.so

例子:

A.hpp:

template <class T>
class A
{
    void foo()
    {
        std::cout << T::K << std::endl;
    }
};

T1.hpp:

class T1
{
    enum { K = 1 };
};

T2.hpp:

class T2
{
    enum { K = 2 };
};

main1.cpp 和 main2.cpp 都调用 A::foo()。 但输出始终为 1。

更新

main1.cpp:

#include "../A.hpp"
#include "T1.hpp"

int main(int argc, char **argv)
{
    A<T1> a;
    a.foo();
    return 0;
}

main2.cpp:

#include "../A.hpp"
#include "T2.hpp"

int main(int argc, char **argv)
{
    A<T2> a;
    a.foo();
    return 0;
}

更新2:

这发生在 mac os x 上。

g++ -v

使用内置规范。 COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin11/4.7.2/lto-wrapper 目标:x86_64-apple-darwin11 配置:../gcc-4.7.2/configure --prefix=/opt/local --build=x86_64-apple-darwin11 --enable-languages=c,c++,objc,obj-c++,lto,fortran, java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man - -datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls - -program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-version-check -- enable-stage1-checking --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar =/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.2_2+universal' 线程模型:posix gcc 版本 4.7.2 (MacPorts gcc47 4.7.2_2+universal)

但是它在 linux 上可以正常工作。

g++ -v

使用内置规范。 COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/gcc_4_7/libexec/gcc/x86_64-linux-gnu/4.7.0/lto-wrapper 目标:x86_64-linux-gnu 配置:../gcc-4.7.0/configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 --with-mpc=/usr/gcc_4_7 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-4.7 线程模型:posix gcc 版本 4.7.0 (GCC)

更新3:

我的解释具有误导性,因为我实际上是在创建具有相同名称的文件(lib1/T.hpp、lib2/T.hpp、..)。 T_i 类型的名称也相同。通过包含相应目录中的 T.hpp 选择了正确的一个。 为 T_i 使用不同的名称(而不是 T_i.hpp)解决了这个问题。但是,我仍然想知道当类型名称相同时出了什么问题。

【问题讨论】:

  • 没有A::foo()。你的意思是A&lt;T_i&gt;::foo()
  • @StoryTeller,是的,当然,我写在正文里了。
  • 是的,我不得不滚动阅读结尾。 :) 无论如何,请发布 main1.cpp 和 main2.cpp。
  • 还有,每个lib_i是如何准确生成的?
  • @StoryTeller,对于每个 lib_i,我使用 system("mkdir .. 创建一个目录。然后在每个目录中,我生成文件 T.hpp 和 main.cpp(它们实际上具有相同的名称,而不是 T1.hpp、T2.hpp 等)。我还将 Makefile 复制到目录中,然后调用 system("make -C ..。最后,我使用dlopendlsym来调用函数。

标签: c++ templates


【解决方案1】:

我试过你的代码,它工作正常。 A&lt;T2&gt; a; a.foo() 将输出 2。

T.hpp 和 main.cpp 同名,恐怕链接错了代码。

您可能希望使用不同的文件名。 :)

【讨论】:

  • 我刚刚在linux机器上试过,也可以。请参阅 UPDATE2。
  • 使用不同的文件名实际上有帮助)但我仍然想知道这种奇怪的行为
  • @Nil 我不确定是哪一个导致了问题,您在不同操作系统中编程的属性或两种环境之间的差异。也许您在程序中包含 lib1 目录,编译器将使用 lib1 中的 T.cpp 作为首选。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 2020-01-28
  • 2011-11-24
  • 2015-10-07
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多