How to link *.lib for windows platform

Hello ,

I am trying to build own custom platform and framwork, everything is ok, but I am stucking on python build script.

I have several *.lib libraries under the Windows platform. I need to link them last to ensure that all objects files can be linked successfully, but I have never been able to do this.
Here I tried two methods, the first is to use the (LIBS) environment variable, but it always links the .lib file with the (-l) prefix

  1. First method
#Build source code , this is the same part.
# THREADX
    threadx_sources_dir = join(framework_dir, platform, core, env.sdk)
    env.BuildSources(
        join("$BUILD_DIR", platform),  # 创建独立的子目录,
        threadx_sources_dir,
        src_filter="+<*.s>"
    )
    # bw
    bw_sources_dir = join(framework_dir, platform, core, "bw")
    env.BuildSources(
        join("$BUILD_DIR","_bw"),  # 创建独立的子目录
        quectel_sources_dir,
        src_filter="+<*.c>"
    )

    # PROJECT
    project_sources_dir = join("$PROJECT_DIR", "lib")
    env.BuildSources(
        join("$BUILD_DIR","project"),  #独立的子目录
        project_sources_dir,
        src_filter="+<*.c>"
    )

#LINK code
lib_path = join(framework_dir, platform, core, env.sdk, "lib")
lib_files = glob.glob(os.path.join(lib_path, '*.lib'))
env.Append(LIBS = lib_files)

#Result
clang++ -o .pio\build\bg96\program.elf -d -target armv7m-none-musleabi -fuse-ld=qcld -lc++ -Wl,-mno-unaligned-access -fuse-baremetal-sysroot -fno-use-baremetal-crt -Wl,-entry=0x43000000 -Wl,-TC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\c.ld -Wl,-Map=C:/Users/bakewell.wang/Documents/PlatformIO/Projects/321/.pio/build/bg96\program.map,-gc-sections -Wl,-gc-sections .pio\build\bg96\threadx\txm_module_preamble_llvm.o .pio\build\bg96\_quectel\quectel_uart_apis.o .pio\build\bg96\_quectel\quectel_utils.o .pio\build\bg96\src\main.o -L.pio\build\bg96 -lC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\diag_dam_lib.lib -lC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\qapi_psm_lib.lib -lC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\timer_dam_lib.lib -lC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\txm_lib.lib
**Fatal: cannot recognize namespec -lC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\diag_dam_lib.lib**
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pio\build\bg96\program.elf] Error 1
  1. Second method
#Code
lib_path = join(framework_dir, platform, core, env.sdk, "lib")
lib_files = glob.glob(os.path.join(lib_path, '*.lib')) 
env.Append(LINKFLAGS = lib_files)


#Result ,Failed due to lib being linked first
clang++ -o .pio\build\bg96\program.elf -d -target armv7m-none-musleabi -fuse-ld=qcld -lc++ -Wl,-mno-unaligned-access -fuse-baremetal-sysroot -fno-use-baremetal-crt -Wl,-entry=0x43000000 -Wl,-TC:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\c.ld -Wl,-Map=C:/Users/bakewell.wang/Documents/PlatformIO/Projects/321/.pio/build/bg96\program.map,-gc-sections -Wl,-gc-sections C:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\diag_dam_lib.lib C:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\qapi_psm_lib.lib C:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\timer_dam_lib.lib C:\Users\bakewell.wang\.platformio\packages\framework-quectel\threadx\bg96\SDK30\lib\txm_lib.lib .pio\build\bg96\threadx\txm_module_preamble_llvm.o .pio\build\bg96\_quectel\quectel_uart_apis.o .pio\build\bg96\_quectel\quectel_utils.o .pio\build\bg96\src\main.o -L.pio\build\bg96
Error: .pio\build\bg96\threadx\txm_module_preamble_llvm.o(.relInit+0x18): undefined reference to `_txm_module_thread_shell_entry'
Error: .pio\build\bg96\threadx\txm_module_preamble_llvm.o(.relInit+0x2c): undefined reference to `_txm_module_callback_request_thread_entry'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x6c): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0xc4): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0xe0): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x130): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x1ec): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x274): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x2f4): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x348): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x390): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x3e0): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x468): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_uart_apis.o(.text+0x484): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\_quectel\quectel_utils.o(.text+0x5c): undefined reference to `_txm_module_system_call11'
Error: .pio\build\bg96\src\main.o(.text+0xb8): undefined reference to `_txm_module_system_call12'
Error: .pio\build\bg96\src\main.o(.text+0xfc): undefined reference to `qapi_Timer_Sleep'
Error: .pio\build\bg96\src\main.o(.text+0x150): undefined reference to `_txm_module_system_call5'
Error: .pio\build\bg96\src\main.o(.text+0x164): undefined reference to `_txm_module_kernel_call_dispatcher'
Error: .pio\build\bg96\src\main.o(.text+0x1a0): undefined reference to `_txm_module_system_call4'
Error: .pio\build\bg96\src\main.o(.text+0x1c0): undefined reference to `_txm_module_system_call4'
Error: .pio\build\bg96\src\main.o(.text+0x280): undefined reference to `_txm_module_kernel_call_dispatcher'
Fatal: Linking had errors.
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pio\build\bg96\program.elf] Error 1

You could try to append an array of (SCons) File() object tothe env["LIBS"] as I do e.g. here.

lib_path = join(framework_dir, platform, core, env.sdk, "lib")
lib_files = glob.glob(os.path.join(lib_path, '*.lib')) 
# Creates array of scons.Node.FS.File object 
env.Append(LIBS=[File(f) for f in lib_files])

Otherwise, the error with the first method

is that the -l flag just has to have the name of the libary, i.e., diag_dam_lib. No extension. (GCC also expects the file to start with lib, which has to be ommited – not sure with Clang). The lib_path in which the .lib files are should be added to env["LIBPATH"] then (generates -L flag).

Hello ,

I am using “llvm-clang(windows)” ,this is my command line build. It can be seen that llvm-clang expects a library path without the (-l) prefix.

InstalledDir: C:\compile_tools\Snapdragon-llvm-4.0.3-windows64\bin

Options for link job construction: --driver-mode=g++ -v -d -o bin\quectel_demo_nmea_usb.elf -target armv7m-none-musleabi -fuse-ld=qcld -lc++ -Wl,-mno-unaligned-access -fuse-baremetal-sysroot -fno-use-baremetal-crt -Wl,-entry=0x40000000 -Wl,-Tquectel\build\quectel_dam_demo.ld -Wl,-Map=bin\quectel_demo_nmea_usb.map,-gc-sections -Wl,-gc-sections quectel\example\build\example_nmea_usb.o quectel\example\build\quectel_std_func.o quectel\example\build\quectel_uart_apis.o quectel\example\build\quectel_utils.o quectel\example\build\txm_module_preamble_llvm.o libs\diag_dam_lib.lib libs\qapi_psm_lib.lib libs\timer_dam_lib.lib libs\txm_lib.lib quectel\example\build\txm_module_preamble_llvm.o

 "C:\\compile_tools\\Snapdragon-llvm-4.0.3-windows64\\bin\\ld.qcld.exe" -X --eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-musl-arm.so.1 -o "bin\\quectel_demo_nmea_usb.elf" -lc++ -mno-unaligned-access -entry=0x40000000 "-Tquectel\\build\\quectel_dam_demo.ld" "-Map=bin\\quectel_demo_nmea_usb.map" -gc-sections -gc-sections "quectel\\example\\build\\example_nmea_usb.o" "quectel\\example\\build\\quectel_std_func.o" "quectel\\example\\build\\quectel_uart_apis.o" "quectel\\example\\build\\quectel_utils.o" "quectel\\example\\build\\txm_module_preamble_llvm.o" **"libs\\diag_dam_lib.lib" "libs\\qapi_psm_lib.lib" "libs\\timer_dam_lib.lib" "libs\\txm_lib.lib"** "quectel\\example\\build\\txm_module_preamble_llvm.o" "-LC:\\compile_tools\\Snapdragon-llvm-4.0.3-windows64\\armv7m-none-eabi\\libc\\lib" "-LC:\\compile_tools\\Snapdragon-llvm-4.0.3-windows64\\armv7m-none-eabi\\lib" --start-group -lunwind -lc++ -lc++abi --end-group "-LC:\\compile_tools\\Snapdragon-llvm-4.0.3-windows64\\lib\\clang\\4.0.3\\lib\\baremetal" --start-group -lc -lclang_rt.builtins-armv7m --end-group -lm -lc
Input File: bin\quectel_demo_nmea_usb.elf [32-bit size: 89720 (0x15e78)]
Output Binary Format: bin\quectel_demo_nmea_usb.bin Bit Width: 8, Bank: 1
== Demo application is built in bin

LIBS requires the -l flag just has to have the name of the libary, i.e., diag_dam_lib. No extension, but clang requires the full path of the library without -l flag, so I am not using LIBS. If there is a way to control dependencies, I can also This helps, for example link *.o first, then link to *.lib

I also used the code env.Append(LIBS=[File(f) for f in lib_files]) ,but return the error information below, what is File?

NameError: name 'File' is not defined:
  File "C:\Users\bakewell.wang\.platformio\penv\Lib\site-packages\platformio\builder\main.py", line 173:
    env.SConscript("$BUILD_SCRIPT")
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Script\SConscript.py", line 609:
    return _SConscript(self.fs, *files, **subst_kw)
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Script\SConscript.py", line 279:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "C:\Users\bakewell.wang\.platformio\platforms\quectel\builder\main.py", line 14:
    elf = env.BuildProgram()
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Util\envs.py", line 251:     
    return self.method(*nargs, **kwargs)
  File "C:\Users\bakewell.wang\.platformio\penv\Lib\site-packages\platformio\builder\tools\piobuild.py", line 61:   
    env.ProcessProgramDeps()
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Util\envs.py", line 251:     
    return self.method(*nargs, **kwargs)
  File "C:\Users\bakewell.wang\.platformio\penv\Lib\site-packages\platformio\builder\tools\piobuild.py", line 121:
    env.BuildFrameworks(env.get("PIOFRAMEWORK"))
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Util\envs.py", line 251:     
    return self.method(*nargs, **kwargs)
  File "C:\Users\bakewell.wang\.platformio\penv\Lib\site-packages\platformio\builder\tools\piobuild.py", line 347:  
    SConscript(env.GetFrameworkScript(name), exports="env")
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Script\SConscript.py", line 673:
    return method(*args, **kw)
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Script\SConscript.py", line 609:
    return _SConscript(self.fs, *files, **subst_kw)
  File "C:\Users\bakewell.wang\.platformio\packages\tool-scons\scons-local-4.6.0\SCons\Script\SConscript.py", line 279:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "C:\Users\bakewell.wang\.platformio\platforms\quectel\builder\frameworks\threadx.py", line 9:
    m.dev_init(env, platform)
  File "C:\Users\bakewell.wang\.platformio\platforms\quectel\builder\frameworks\threadx-bg96.py", line 237:
    env.Append(LIBS=[File(f) for f in lib_files])
  File "C:\Users\bakewell.wang\.platformio\platforms\quectel\builder\frameworks\threadx-bg96.py", line 237:
    env.Append(LIBS=[File(f) for f in lib_files])

I am sucessfully with the code -env.Append(LIBS=[env.File(f) for f in lib_files])

Thanks,