Building/Compiling/Exporting C++ Native

ThrallherdThrallherd Posts: 2Member

Hello, I've already raised an issue here but I was hoping to find more information on the forum.

My project compiles fine with Linux. If I want to build/compile it for Android, it is my understanding that I need to:

  1. run the SConstruct file inside godot-cpp/ to build it
  2. run the SConstruct file outside godot-cpp/ to compile it

Unfortunately I have failed on both accounts so far. I am aware that the two SConstruct files are not the same one. Inside godot-cpp/, this is what happens when I choose platform=android:

$ scons platform=android target=release android_arch=arm64v8
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
/home/lol/Android/Sdk/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -o src/core/PoolArrays.o -c --target=aarch64-linux-android21 -march=armv8-a -fPIC -I. -Igodot_headers -Iinclude -Iinclude/gen -Iinclude/core src/core/PoolArrays.cpp
sh: 1: /home/lol/Android/Sdk/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++: not found
scons: *** [src/core/PoolArrays.o] Error 127
scons: building terminated because of errors.

I've tried to install Clang in the SDK but I failed to find where that was an option.

Outside godot-cpp/, this is what happens when I choose platform=android:

$ scons platform=android target=release android_arch=arm64v8
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o demo/bin/libhexworld.so -shared src/GDLibrary.os -Lgodot-cpp/bin -lgodot-cpp.release.64
/usr/bin/ld: cannot find -lgodot-cpp.release.64
collect2: error: ld returned 1 exit status
scons: *** [demo/bin/libhexworld.so] Error 1
scons: building terminated because of errors.

Could you please help me? My OS is Ubuntu 18.04 LTS and I don't know what to do.


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,666Admin

    Welcome to the forums @Thrallherd!

    I have not really tried building a GDNative project yet, but I'll try to help:

    The first error seems to be that the clang++ executable is not located at /home/lol/Android/Sdk/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++, which is causing it to crash. If you navigate to the Android sdk/ndk folder and search for clang++, do you get any results? Maybe the scons script is just pointing to the wrong directory for the install of Ubuntu you have.

    From what I can gather from Google, it appears clang++ is part of the Android NDK, not the SDK. I might try reinstalling the Android NDK if you have not tried that already and see if that helps.


    With the second issue, I'm not totally sure what is going on to be honest. It seems that it is looking for a compiled C++ library (.so file) but cannot find it, and therefore crashes. The library it looks like it cannot find is called GDLibrary.so, from what I can gather from the error message.

  • ThrallherdThrallherd Posts: 2Member

    Hello, thank you for the reply. There is no clang file anywhere in my Android folder. I'm not sure how to fix SConstruct about that... Here's the relevant part:

    elif env['platform'] == 'android':
        if host_platform == 'windows':
            env = env.Clone(tools=['mingw'])
            env["SPAWN"] = mySpawn
    
        # Verify NDK root
        if not 'ANDROID_NDK_ROOT' in env:
            raise ValueError("To build for Android, ANDROID_NDK_ROOT must be defined. Please set ANDROID_NDK_ROOT to the root folder of your Android NDK installation.")
    
        # Validate API level
        api_level = int(env['android_api_level'])
        if env['android_arch'] in ['x86_64', 'arm64v8'] and api_level < 21:
            print("WARN: 64-bit Android architectures require an API level of at least 21; setting android_api_level=21")
            env['android_api_level'] = '21'
            api_level = 21
    
        # Setup toolchain
        toolchain = env['ANDROID_NDK_ROOT'] + "/toolchains/llvm/prebuilt/"
        if host_platform == "windows":
            toolchain += "windows"
            import platform as pltfm
            if pltfm.machine().endswith("64"):
                toolchain += "-x86_64"
        elif host_platform == "linux":
            toolchain += "linux-x86_64"
        elif host_platform == "osx":
            toolchain += "darwin-x86_64"
        env.PrependENVPath('PATH', toolchain + "/bin") # This does nothing half of the time, but we'll put it here anyways
    
        # Get architecture info
        arch_info_table = {
            "armv7" : {
                "march":"armv7-a", "target":"armv7a-linux-androideabi", "tool_path":"arm-linux-androideabi", "compiler_path":"armv7a-linux-androideabi",
                "ccflags" : ['-mfpu=neon']
                },
            "arm64v8" : {
                "march":"armv8-a", "target":"aarch64-linux-android", "tool_path":"aarch64-linux-android", "compiler_path":"aarch64-linux-android",
                "ccflags" : []
                },
            "x86" : {
                "march":"i686", "target":"i686-linux-android", "tool_path":"i686-linux-android", "compiler_path":"i686-linux-android",
                "ccflags" : ['-mstackrealign']
                },
            "x86_64" : {"march":"x86-64", "target":"x86_64-linux-android", "tool_path":"x86_64-linux-android", "compiler_path":"x86_64-linux-android",
                "ccflags" : []
            }
        }
        arch_info = arch_info_table[env['android_arch']]
    
        # Setup tools
        env['CC'] = toolchain + "/bin/clang"
        env['CXX'] = toolchain + "/bin/clang++"
        env['AR'] = toolchain + "/bin/" + arch_info['tool_path'] + "-ar"
    
        env.Append(CCFLAGS=['--target=' + arch_info['target'] + env['android_api_level'], '-march=' + arch_info['march'], '-fPIC'])#, '-fPIE', '-fno-addrsig', '-Oz'])
        env.Append(CCFLAGS=arch_info['ccflags'])
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file