代码之家  ›  专栏  ›  技术社区  ›  bremen_matt

设置Swig的编译器标志

  •  0
  • bremen_matt  · 技术社区  · 7 年前

    我正在尝试使用Swig为一个大型C++项目生成接口文件。项目的头文件根据编译器类型进行一些检查,例如,

    #if defined(__clang__)
       ...
    #if defined(__llvm__)
    

    swig -D__clang__ ... 
    

    然而,这似乎并没有减轻我收到的错误消息,因为我仍然会收到相同的错误。

    1. 我是否通过调用

    或者我应该在xxx中定义这些参数吗。i接口文件?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Paolo Brandoli    7 年前

    你应该有一个。定义要生成的接口的文件。

    在那里你可以简单地使用 #define __clang__ .

    例如,看看一大口。我申请了一个图书馆 https://bitbucket.org/binarno/imebra/src/db648a8b9c359524d40fd7d63c785b6269147010/wrappers/swig.i?at=default

    还要注意,swig不需要所有编译器标志:它不会编译您的项目,但它只会生成额外的cxx文件。在中。我只声明允许swig选择正确类和方法的标志(如果需要)。

        2
  •  0
  •   bremen_matt    7 年前

    -dM 命令(至少 clang gcc 似乎)。在我的例子中,我想看看哪些预处理器宏是由Android构建系统设置的。这有点复杂,因为Android NDK有许多编译器,所有编译器的构建都略有不同。因此,我创建了以下python 3脚本,以将所有宏包含保存到文件中:

    import os, sys
    
    # Define the toolchain root
    root = "C:\\Android\\android-sdk\\ndk-bundle\\toolchains\\"
    
    # Define the directory where you want all of the macro definitions to go
    output = "output\\"
    os.makedirs(output, exist_ok=True)
    
    # Get a list of all of the files
    files = [ ( os.path.join(dp, f), f ) for dp, dn, fn in os.walk(os.path.expanduser(root)) for f in fn ]
    
    # We only want executables 
    files = [ file for file in files if file[1].endswith("exe") ]
    
    # Now, let's limit that to executables that contain one of the following keys
    keys = [ "gcc", "g++", "clang", "c++", "cpp" ]
    files = [ file for file in files if any(key in file[1] for key in keys) ]
    
    # and that exclude the following keys
    keys = [ "-ar", "-nm", "-ranlib", "filt", "-format", "-tidy" ]
    files = [ file for file in files if all(key not in file[1] for key in keys) ]
    
    # Now, for each of these files, we want to see what preprocessor macros are being set. 
    call = " -dM -E - < NUL > "
    for file in files:
        command = file[0] + call + output + file[1].replace(".exe", ".txt" ).replace(root,"")
        print(command)
        os.system( command )
    

    python introspect.py
    

    这个脚本所做的是查找所有 *.exe 文件(对于linux系统更改此选项) root 目录(在我的系统上 C:\\Android\\android-sdk\\ndk-bundle\\toolchains\\ )它应该在某个地方包含编译器。具体来说,该脚本将查找具有其中一个术语的所有可执行文件

    [ "gcc", "g++", "clang", "c++", "cpp" ] 
    

    在名称中,不包括具有

    [ "-ar", "-nm", "-ranlib", "filt", "-format", "-tidy" ] 
    

    以我的名义。最后,找到的每个可执行文件都以以下格式运行

    compiler.exe -dM -E - < NUL > output\compiler.txt
    

    compiler 会是这样的 叮当声 g++ output 目录在我的系统上运行后 目录包含文件(警告:通过NDK确实有很多编译器可用):

    aarch64-linux-android-c++.txt
    aarch64-linux-android-cpp.txt
    aarch64-linux-android-g++.txt
    aarch64-linux-android-gcc-4.9.txt
    aarch64-linux-android-gcc-4.9.x.txt
    aarch64-linux-android-gcc.txt
    arm-linux-androideabi-c++.txt
    arm-linux-androideabi-cpp.txt
    arm-linux-androideabi-g++.txt
    arm-linux-androideabi-gcc-4.9.txt
    arm-linux-androideabi-gcc-4.9.x.txt
    arm-linux-androideabi-gcc.txt
    clang++.txt
    clang.txt
    clang_32.txt
    i686-linux-android-c++.txt
    i686-linux-android-cpp.txt
    i686-linux-android-g++.txt
    i686-linux-android-gcc-4.9.txt
    i686-linux-android-gcc-4.9.x.txt
    i686-linux-android-gcc.txt
    mips64el-linux-android-c++.txt
    mips64el-linux-android-cpp.txt
    mips64el-linux-android-g++.txt
    mips64el-linux-android-gcc-4.9.txt
    mips64el-linux-android-gcc-4.9.x.txt
    mips64el-linux-android-gcc.txt
    mipsel-linux-android-c++.txt
    mipsel-linux-android-cpp.txt
    mipsel-linux-android-g++.txt
    mipsel-linux-android-gcc-4.9.txt
    mipsel-linux-android-gcc-4.9.x.txt
    mipsel-linux-android-gcc.txt
    x86_64-linux-android-c++.txt
    x86_64-linux-android-cpp.txt
    x86_64-linux-android-g++.txt
    x86_64-linux-android-gcc-4.9.txt
    x86_64-linux-android-gcc-4.9.x.txt
    x86_64-linux-android-gcc.txt
    

    #define __DBL_MIN_EXP__ (-1021)
    #define __UINT_LEAST16_MAX__ 65535
    #define __ATOMIC_ACQUIRE 2
    #define __FLT_MIN__ 1.17549435082228750796873653722224568e-38F
    #define __GCC_IEC_559_COMPLEX 2
    #define __UINT_LEAST8_TYPE__ unsigned char
    #define __INTMAX_C(c) c ## L
    #define __CHAR_BIT__ 8
    #define __UINT8_MAX__ 255
    #define __ANDROID__ 1
    #define __WINT_MAX__ 4294967295U
    #define __ORDER_LITTLE_ENDIAN__ 1234
    #define __SIZE_MAX__ 18446744073709551615UL
    #define __WCHAR_MAX__ 4294967295U
    #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
    #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
    #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
    

    事实证明,这些文件中有许多是相同的,但它们之间存在显著差异 叮当声 通用条款