Compiling Android dynamic library encounters the problem of generating import library

question

When using scons to compile the Android dynamic library on Windows, an error occurs when linking the dynamic library:

D:\android\android-sdk\ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++ --target=aarch64-linux-android21 -march=armv8-a -shared -o src\android\ddsnative.so … -Wl,–out-implib,src\android\libddsnative.a
=====
ld: error: unknown argument ‘–out-implib’

Corresponding Scons script:

if env["platform"] == "android":
    if host_platform == "windows":
        # Don't Clone the environment. Because otherwise, SCons will pick up msvc stuff.
        env = Environment(ENV=os.environ, tools=["mingw"])
        opts.Update(env)
...
library = None
library = env.SharedLibrary(target=env["target_path"] + "/" + env["platform"] + "/" + env["target_name"], source=sources)
Default(library)

The penultimate sentence "env.SharedLibrary()" is the key to triggering the link command and related parameters.
When linking Android dynamic libraries, you do not need to generate an import library like Windows dynamic libraries, so the corresponding command line parameters "-Wl, --out-implib" are not accepted.

investigation

  1. If you replace mingw in the Environment () parameter in the fourth line of the code with other clangxx or the like, an error will be reported:

AttributeError: ‘SConsEnvironment’ object has no attribute ‘SharedLibrary’

The side effects are huge and it’s too troublesome to change.

  1. Check the Scons code and confirm that the "-Wl,–out-implib" command line parameter is added to the shlib_generator () function in \Python3\Lib\site-packages\SCons\Tool\mingw.py :
def shlib_generator(target, source, env, for_signature):
...
    implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
    if implib: cmd.append('-Wl,--out-implib,' + implib.get_string(for_signature))

It is not advisable to modify mingw.py directly. Modifying LIBPREFIX or LIBSUFFIX will have no effect on the results.

  1. I accidentally discovered this code in mingw.py:
def shlib_emitter(target, source, env):
    dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
    no_import_lib = env.get('no_import_lib', 0)
...
    if not no_import_lib and \
            not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):

If you disable the generation of import libraries through no_import_lib, there won't be any redundant command line parameters, right?

Solution

Modify the Scons script:

	env = Environment(ENV=os.environ, tools=["mingw"])
+	env["no_import_lib"] = True
	opts.Update(env)

In addition, you can also add the scons command line parameter "no_import_lib=yes" and use the "opts.Update(env)" statement to set the env environment variable function to solve this problem.

Guess you like

Origin blog.csdn.net/feiyunw/article/details/127524605