Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将D库链接到C++;代码_C++_D_Undefined Reference_Multiple Definition Error - Fatal编程技术网

C++ 将D库链接到C++;代码

C++ 将D库链接到C++;代码,c++,d,undefined-reference,multiple-definition-error,C++,D,Undefined Reference,Multiple Definition Error,最近我学习了漂亮的语言D,它更具可塑性,有助于自己编写稳定快速的程序。但它不受欢迎。。。因为在D上编写的代码很少,所以C和C++的代码比较多。因此,在我读了Andrei Alexanderscu的著作之后,作者在这里非常详细地描述了关于D库与C++代码的连接的问题,我尝试自己学习,并在D中编写了一些代码,其中定义函数返回完全自动机类的实例,该类实现了CutoAcAutoCub,也用C++代码定义: #ifndef AUTOMATAINTERFACE_H #define AUTOMATAINTER

最近我学习了漂亮的语言D,它更具可塑性,有助于自己编写稳定快速的程序。但它不受欢迎。。。因为在D上编写的代码很少,所以C和C++的代码比较多。因此,在我读了Andrei Alexanderscu的著作之后,作者在这里非常详细地描述了关于D库与C++代码的连接的问题,我尝试自己学习,并在D中编写了一些代码,其中定义函数返回完全自动机类的实例,该类实现了CutoAcAutoCub,也用C++代码定义:

#ifndef AUTOMATAINTERFACE_H
#define AUTOMATAINTERFACE_H

class AutomataInterface {
public:
    virtual ~AutomataInterface() {}

    virtual void next() = 0;

    virtual void save() = 0;
    virtual void restore() = 0;
    virtual void zerofile() = 0;

    virtual void invert(unsigned long x, unsigned long y) = 0;

    virtual int state(unsigned long x, unsigned long y) const = 0;

    virtual unsigned long x() const = 0;
    virtual unsigned long y() const = 0;
};

AutomataInterface *createAutomata(unsigned long x, unsigned long y);

#endif // AUTOMATAINTERFACE_H
相关D代码:

import agregator; // this is my own lib

extern(C++) {
    interface AutomataInterface {
        void next();

        void save();
        void restore();
        void zerofile();

        void invert(size_t x, size_t y);

        int state(size_t x, size_t y) const;

        size_t x() const;
        size_t y() const;
    }

    AutomataInterface createAutomata(ulong x, ulong y) {
        return new CompleteAutomata(x, y);
    }
}

export class CompleteAutomata : AutomataInterface {
    // instance variables...
    this(size_t x, size_t y) { /* ... */ }

    extern(C++) {
        override void next() {
            // ...
        }

        // others overridden interface methods...
    }
}
代码编写完成后,我使用两个不同的编译器(
dmd
gdc
)编译了D库,并带有以下标志:

dmd -release -O -lib -odlib -ofliblife.h *.d

当我尝试通过向库目录添加路径(
-L
选项)并直接添加库(
-L
选项)将每个接收到的库链接到Qt项目时。在这两种情况下,我都犯了错误

在第一个
dmd
案例中,我有“未定义的对`_d_newclass`的引用”和另外两个错误:

g++ -Wl,-O1 -Wl,-z,relro -o automata main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread 
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1fe_5b0.o): In function `createAutomata(unsigned int, unsigned int)':
complete_automata.d:(.text._Z14createAutomatajj+0x27): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x50): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0xd0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x120): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x68): undefined reference to `_D6object6Object8toStringMFZAya'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x70): undefined reference to `_D6object6Object6toHashMFNbNeZm'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x78): undefined reference to `_D6object6Object5opCmpMFC6ObjectZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x80): undefined reference to `_D6object6Object8opEqualsMFC6ObjectZb'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0xf8): undefined reference to `_D6object6Object8toStringMFZAya'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x100): undefined reference to `_D6object6Object6toHashMFNbNeZm'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x108): undefined reference to `_D6object6Object5opCmpMFC6ObjectZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x110): undefined reference to `_D6object6Object8opEqualsMFC6ObjectZb'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `_D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata':
complete_automata.d:(.text._D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata+0x1f): undefined reference to `_d_newclass'
complete_automata.d:(.text._D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata+0x46): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::next()':
complete_automata.d:(.text._ZN16CompleteAutomata4nextEv+0x2f): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::save()':
complete_automata.d:(.text._ZN16CompleteAutomata4saveEv+0x25): undefined reference to `_adDupT'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::restore()':
complete_automata.d:(.text._ZN16CompleteAutomata7restoreEv+0x33): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::zerofile()':
complete_automata.d:(.text._ZN16CompleteAutomata8zerofileEv+0x2f): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `no symbol':
/usr/include/dmd/druntime/import/object.di:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o):(.data._D12TypeInfo_Axi6__initZ+0x0): undefined reference to `_D14TypeInfo_Array6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5137__arrayZ':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5137__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5138__assertFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5138__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.51315__unittest_failFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.51315__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `no symbol':
/usr/include/dmd/druntime/import/object.di:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o):(.data._D11TypeInfo_xi6__initZ+0x0): undefined reference to `_D14TypeInfo_Const6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o):(.data._D11TypeInfo_xi6__initZ+0x10): undefined reference to `_D10TypeInfo_i6__initZ'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5157__arrayZ':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5157__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5158__assertFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5158__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.51515__unittest_failFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.51515__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `no symbol':
agregator.d:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o):(.data+0x10): undefined reference to `_D3std6random12__ModuleInfoZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o):(.rodata+0x20): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator7__arrayZ':
agregator.d:(.text._D9agregator7__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator8__assertFiZv':
agregator.d:(.text._D9agregator8__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator15__unittest_failFiZv':
agregator.d:(.text._D9agregator15__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.data+0x0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.data+0x50): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.rodata+0x48): undefined reference to `_D6object6Object8toStringMFZAya'
...
在第二种情况下(使用
gdc
),我收到关于“多重定义”的消息:

经过两天的尝试后…

最近我尝试将Phobos(D标准库)添加到链接过程中。对于
dmd
-lphobos2标志和
gdc
-lgphobos2标志对应。但这对我没有帮助

使用
dmd
链接器输出时:

g++ -Wl,-O1 -Wl,-z,relro -o cellular_life main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread -lphobos2
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_duphandle@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_strerror@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_slist_free_all@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_global_init@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_perform@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_init@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_pause@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `_Dmain'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_setopt@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_slist_append@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_global_cleanup@CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_cleanup@CURL_GNUTLS_3'
collect2: error: ld returned 1 exit status
make: *** [cellular_life] Error 1
我尝试了libcurl gnutls的替换:
ln-s/usr/lib64/libcurl.so.4/usr/lib64/libcurl gnutls.so.4
。然后是链接相同但没有关于libcurl gnutls的消息的结果

在使用
gdc
链接器输出时,再次谈论“多定义到”:

最后是关于librt.so.1的消息。我检查了/usr/lib64并在那里看到了它的库文件


<亲爱的魔法>,请告诉我如何将D库连接到C++代码。

,一般来说,让D编译器做链接:

比较容易。
 g++ -c yourfile.cpp
 dmd yourfile.o d_file.d
您可能还需要向其添加curl,与以前一样。要通过dmd传递链接器参数(如果您使用的是gdc,则应使用相同的方式传递gdmd),请传递-Larg

 dmd yourfile.o d_file.d -L-lstdc++ -L-lcurl -L-lQtGui # and so on
<>通常在主语中放置主语()也会(它也可以立即调用C++定义函数),否则,在使用C++之前,可能需要初始化D运行时。 但要完成你开始的过程。。。第一件事,看起来像你的liblife.a添加了两次相同的文件。我会尝试删除该文件并重新创建它,或者跳过该步骤直接将.o文件传递给链接器,而不首先将它们打包到.a中。这会使事情简化一点

我的另一个问题是:它为什么要拉D干管?你的.d代码中有main()吗?如果是这样,那就行了,但是你必须从C++代码中删除这个。(可能将其重命名为cppmain,然后从D main调用它:

D代码:

extern(C++) int cppmain(int argc, char** argv);

int main() {
        import core.runtime;
        return cppmain(Runtime.cArgs.argc, Runtime.cArgs.argv);
}
extern(C++) void initD() {
      import core.runtime;
       Runtime.initialize();
}
<>,这将转发到C++主。如果你想删除D主(假设它在那里,如果不让我知道,我会尝试想想还有什么会导致链接错误),在头使用C++代码之前,你会想初始化D:S:

D代码:

extern(C++) int cppmain(int argc, char** argv);

int main() {
        import core.runtime;
        return cppmain(Runtime.cArgs.argc, Runtime.cArgs.argv);
}
extern(C++) void initD() {
      import core.runtime;
       Runtime.initialize();
}
C++代码:

#ifndef AUTOMATAINTERFACE_H
#define AUTOMATAINTERFACE_H

class AutomataInterface {
public:
    virtual ~AutomataInterface() {}

    virtual void next() = 0;

    virtual void save() = 0;
    virtual void restore() = 0;
    virtual void zerofile() = 0;

    virtual void invert(unsigned long x, unsigned long y) = 0;

    virtual int state(unsigned long x, unsigned long y) const = 0;

    virtual unsigned long x() const = 0;
    virtual unsigned long y() const = 0;
};

AutomataInterface *createAutomata(unsigned long x, unsigned long y);

#endif // AUTOMATAINTERFACE_H
extern "C++" void initD();
int main() {
      initD();
      // the rest of your stuff
 }
如果不这样做,调用D函数可能会导致SEGD故障


但是,我很确定你有一个复制文件。O文件添加到你的存档文件中,并且主要在D和C++中定义。删除存档中的副本并杀死其中一个重复的主程序,你应该有一些成功。

在我看到你的答案之前,我发现链接器需要使用<代码> DMD < /Code >。我尝试在M时链接代码。在C++的代码中描述了AIN函数。在这种情况下,我得到了二进制,当它启动时,它得到了Sebug,现在你在读到你的文章之后,我试图在D代码中创建一个main函数,并从它中调用Cppmain函数,在这样的情况下,在连接之后,我也得到二进制,也就是当开始得到一个StEngEngy,但THI。s时间回溯的内容更丰富,它指示在向D类方法完成自动引用时出现错误,从C++到D的时候,通常的错误是D类总是需要堆分配,所以确保你正在做:完全自动机A =新的完整原子()。,而不仅仅是CompleteAutomata;。原因是D中的类总是通过引用完成的(D中的MyClass a最像C++中的MyClass*a)如果你不使用新的初始化,那么引用在使用时会是空的和分段的。另外,D类应该是D的新的,C++类应该用C++来构造。这是有办法的,但是语言不知道如何调用对方的构造函数,所以你必须手动地初始化它。不过,仅仅用他们的母语创建它要容易得多。我使用createAutomata函数创建一个CompleteAutomata对象,正如我在文章中所描述的(by
new
)这个函数用C++代码声明,C++调用了函数,结果被分配给一个指针,它通过一个指针调用一个类的方法,导致StEngEnter。我下载了你的代码,并能让它链接和运行。如果你不能链接,请告诉我,我会。写下这一点。但是我发现C++代码中有两个问题:1)AutoTaaAutoal.h列出了一个虚拟析构函数——不应该存在,所以只需注释一下。这就是导致分段的原因,C++和D在虚拟函数的数目上不一致,所以一切都被关闭了。第二个错误是在~RenderArea中,您“删除”\u cellular”-不要这样做,因为它是在D中创建的,所以D垃圾收集器管理它。手动删除将导致双重空闲。