首页 > 解决方案 > 编译程序并链接所有库和包含路径

问题描述

我正在尝试编译一个 OpenCascade 程序。

这是程序的链接: https://www.opencascade.com/content/unable-convert-step-file-stl-file (这是一个错误的程序,但它是一个开始)

我应该将所有库、库路径和包含路径链接到 gcc(-L、-l、-I 标志)。我已经安装了 OpenCascade,这里是安装的文件夹。

在此处输入图像描述

您看到的这些文件夹中的大多数都有一个 bin、include 和 lib 文件夹。

我是否必须将所有这些链接到编译器才能编译程序?

这些是程序使用的唯一包括:

#include "STEPControl_Reader.hxx"
#include <TopoDS_Shape.hxx>
#include <StlAPI_Writer.hxx>

编辑:“ TopoDS_Shape.hxx”和“ StAPI_Writer.hxx”位于此路径中C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc

' STEPControl_Reader.hxx' 也存在于同一目录中,我不知道为什么原始作者将它放在他的本地目录中。

编辑 2:我也阅读了这个论坛主题: https ://forum.freecadweb.org/viewtopic.php?t=15993 但它对我一点帮助都没有。他使用的是linux,include和lib目录的结构不一样。

编译尝试后编辑

因此,我发出了这个命令:

gcc -I C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc -L C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\win64\vc14\lib -l TKSTEP -l TKBRep -l TKSTL testCode.c

这就是我得到的:

In file included from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Integer.hxx:18,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Address.hxx:18,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d.hxx:21,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/STEPCon
trol_Reader.hxx:20,
                 from testCode.c:2:
C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standard_Std.hxx:20:10: fatal
 error: type_traits: No such file or directory
 #include <type_traits>
          ^~~~~~~~~~~~~
compilation terminated.

编译实验 2

C:\Users\User1\Desktop\OPENCAS>g++ -I C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7
.4.0\inc -L C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\win64\vc14\lib -l TKS
TEP -l TKBRep -l TKSTL testCode.cpp
testCode.cpp: In function 'Standard_Integer main(int, char**)':
testCode.cpp:26:3: error: 'cout' was not declared in this scope
   cout << argv[2] << endl;
   ^~~~
testCode.cpp:26:3: note: suggested alternative:
In file included from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Stream.hxx:20,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_OStream.hxx:19,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_ExtCharacter.hxx:28,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_PrimitiveTypes.hxx:27,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Transient.hxx:20,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d.hxx:91,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/STEPCon
trol_Reader.hxx:20,
                 from testCode.cpp:2:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86
_64-w64-mingw32/8.1.0/include/c++/iostream:61:18: note:   'std::cout'
   extern ostream cout;  /// Linked to standard output
                  ^~~~
testCode.cpp:26:22: error: 'endl' was not declared in this scope
   cout << argv[2] << endl;
                      ^~~~
testCode.cpp:26:22: note: suggested alternative:
In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-re
v0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Stream.hxx:20,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_OStream.hxx:19,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_ExtCharacter.hxx:28,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_PrimitiveTypes.hxx:27,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d_Transient.hxx:20,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/Standar
d.hxx:91,
                 from C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\inc/STEPCon
trol_Reader.hxx:20,
                 from testCode.cpp:2:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86
_64-w64-mingw32/8.1.0/include/c++/ostream:590:5: note:   'std::endl'
     endl(basic_ostream<_CharT, _Traits>& __os)

编译实验 3(将 and 更改coutendlandstd::cout之后std::endl

  C:\Users\User1\Desktop\OPENCAS>g++ -I C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7
    .4.0\inc -L C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\win64\vc14\lib -l TKS
    TEP -l TKBRep -l TKSTL testCode.cpp
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x27): undefine
    d reference to `STEPControl_Reader::STEPControl_Reader()'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x3a): undefine
    d reference to `XSControl_Reader::ReadFile(char const*)'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x49): undefine
    d reference to `STEPControl_Reader::NbRootsForTransfer()'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x58): undefine
    d reference to `XSControl_Reader::TransferRoots()'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x6b): undefine
    d reference to `XSControl_Reader::OneShape() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0x77): undefine
    d reference to `StlAPI_Writer::StlAPI_Writer()'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text+0xa0): undefine
    d reference to `StlAPI_Writer::Write(TopoDS_Shape const&, char const*)'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN24NCollectio
    n_BaseSequencedlEPv[_ZN24NCollection_BaseSequencedlEPv]+0x11): undefined referen
    ce to `Standard::Free(void*)'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI25NCollection_BaseAllocatorE8EndScopeEv[_ZN11opencascade6handleI25NColl
    ection_BaseAllocatorE8EndScopeEv]+0x23): undefined reference to `Standard_Transi
    ent::DecrementRefCounter() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI30TopLoc_SListNodeOfItemLocationE8EndScopeEv[_ZN11opencascade6handleI30
    TopLoc_SListNodeOfItemLocationE8EndScopeEv]+0x23): undefined reference to `Stand
    ard_Transient::DecrementRefCounter() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI13TopoDS_TShapeE8EndScopeEv[_ZN11opencascade6handleI13TopoDS_TShapeE8En
    dScopeEv]+0x23): undefined reference to `Standard_Transient::DecrementRefCounter
    () const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN20NCollectio
    n_SequenceIN11opencascade6handleI18Standard_TransientEEE5ClearERKNS1_I25NCollect
    ion_BaseAllocatorEE[_ZN20NCollection_SequenceIN11opencascade6handleI18Standard_T
    ransientEEE5ClearERKNS1_I25NCollection_BaseAllocatorEE]+0x1f): undefined referen
    ce to `NCollection_BaseSequence::ClearSeq(void (*)(NCollection_SeqNode*, opencas
    cade::handle<NCollection_BaseAllocator>&))'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI21XSControl_WorkSessionE8EndScopeEv[_ZN11opencascade6handleI21XSControl
    _WorkSessionE8EndScopeEv]+0x23): undefined reference to `Standard_Transient::Dec
    rementRefCounter() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN20NCollectio
    n_SequenceI12TopoDS_ShapeE5ClearERKN11opencascade6handleI25NCollection_BaseAlloc
    atorEE[_ZN20NCollection_SequenceI12TopoDS_ShapeE5ClearERKN11opencascade6handleI2
    5NCollection_BaseAllocatorEE]+0x1f): undefined reference to `NCollection_BaseSeq
    uence::ClearSeq(void (*)(NCollection_SeqNode*, opencascade::handle<NCollection_B
    aseAllocator>&))'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI25NCollection_BaseAllocatorE10BeginScopeEv[_ZN11opencascade6handleI25NC
    ollection_BaseAllocatorE10BeginScopeEv]+0x23): undefined reference to `Standard_
    Transient::IncrementRefCounter() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.text$_ZN11opencascad
    e6handleI18Standard_TransientE8EndScopeEv[_ZN11opencascade6handleI18Standard_Tra
    nsientE8EndScopeEv]+0x23): undefined reference to `Standard_Transient::Decrement
    RefCounter() const'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.rdata$.refptr._ZTV18
    STEPControl_Reader[.refptr._ZTV18STEPControl_Reader]+0x0): undefined reference t
    o `vtable for STEPControl_Reader'
    C:\Users\User1\AppData\Local\Temp\ccCCS9c3.o:testCode.cpp:(.rdata$.refptr._ZTV16
    XSControl_Reader[.refptr._ZTV16XSControl_Reader]+0x0): undefined reference to `v
    table for XSControl_Reader'
    collect2.exe: error: ld returned 1 exit status

标签: gcccompilationinclude-pathlibopencascade

解决方案


我是否必须将所有这些链接到编译器才能编译程序?

OCCT 是一个框架,它由分组为模块的工具包(库)组成。可以链接到整个 OCCT 框架(所有库),但在这种情况下,未使用的库将成为无用的负担。相反,最好只链接和传送应用程序实际使用的工具包,以及它们的嵌套依赖项。

链接和编译是构建过程的两个不同步骤,因此:

  • 您需要通过-I参数将头文件的位置传递给编译器,该参数通常是一个包含公共 OCCT 类的所有头文件的inc文件夹。这在自定义构建中可能有所不同。
  • 您需要通过-L参数将导入库的位置传递给链接器,对于 Release通常是win64/vc14/lib ,对于调试版本的库通常是 win64 /vc14/libd ,其中win64表示平台(64 位 Windows),vc14表示编译器(视觉工作室 2015);如果是 MinGW ,它将是gcc而不是vc14
  • 您需要通过-l参数将导入库的名称传递给链接器(或者,它可能是每个库的完整路径以跳过-L参数)。
  • 类似的文件夹win64/vc14/binwin64/vc14/bind包含启动应用程序所需的 DLL 库文件。必要的 DLL 应复制到应用程序文件夹或放入Windows 上的%PATH%环境变量中。

用于构建 OCCT 的编译器和配置应与构建应用程序本身的配置相匹配。例如,您不能使用 Visual Studio 构建的 OCCT 来使用 MinGW 构建应用程序 - 它们使用 C++ 内部的二进制不兼容格式。

注意:在 Windows 平台上,链接到不解析应用程序中任何符号的库是无操作的 - 额外的 DLL 不会被放入启动应用程序所需的应用程序依赖项中。这在其他平台(Linux / UNIX)上是不同的,即使没有实际使用,链接库也会被依赖。

如果不研究 OCCT 框架结构,确定您的应用程序需要哪些库可能会很棘手。以下提示可能会有所帮助:

  1. 确定所需库的第一步是检查项目使用的 OCCT 类的驻留。这可以通过两种方式完成:

    • 在生成的Doxygen 文档中查找类(如STEPControl_Reader ),并从路径到类中清除 Toolkit,如“Module DataExchange -> Toolkit TKSTEP -> Package STEPControl”。TKSTEP是您需要链接的库名称(例如-lTKSTEP)。 OCCT Doxygen 中的 STEPControl_Reader 类
    • 在 OCCT 源代码中,grep src文件夹中所有名为PACKAGES的文件以获取所需的包名称(可以很容易地从类名STEPControl_Reader分解-> STEPControl是一个包)。在这种特殊情况下,您将在文件src/ TKSTEP /PACKAGES中找到包STEPControl,并且文件夹名称TKSTEP将是您需要链接到的工具包/库名称。
  2. 在下一步,您将需要确定应用程序隐式使用的其他 OCCT 工具包。例如,大多数 OCCT 类都继承了Tkernel库中定义的Standard_Transient类。尝试构建您的应用程序并检查链接器错误 - 它们应该包含来自 OCCT 框架的类方法的路径。推断类名称 -> 包名称并重复第一步以确定工具包名称。

  3. 作为第 2 步的快捷替代方法,查看Doxygen 文档src/ToolkitName/ EXTENLIB文件中已经推导出的 OCCT 工具包的依赖关系图。 OCCT Doxygen 中的 TKSTEP 依赖项

  4. 最后,您可能需要链接和复制 OCCT 本身使用的第 3 方库,如 FreeType、FreeImage 等。该列表将取决于使用的 OCCT 组件和在构建 OCCT 本身时启用的第 3 方(其中大多数是可选的)。


推荐阅读