xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
9.82k stars 774 forks source link

add_deps不能自动链接 #5046

Open maxchen32 opened 4 months ago

maxchen32 commented 4 months ago

Xmake 版本

v2.9.1+HEAD.0c79f6162

操作系统版本和架构

Windows 8.1

描述问题

我的xmake.lua如下

add_rules("mode.debug", "mode.release")

if is_mode("debug") then
    add_defines("DEBUG")
    set_symbols("debug")
    set_warnings("all")
elseif is_mode("release") then
    set_strip("all")
end

if not is_os("windows") then
    add_syslinks("m")
end

target("objs")
    set_kind("object")
    set_warnings("all")
    add_includedirs("include", {public = true})
    add_files("src/*.c")
    add_cflags("-fPIC")

target("radicalite_static")
    set_kind("static")
    set_warnings("all")
    add_deps("objs")
    set_basename("radical")
    set_prefixname("lib")

target("radicalite_shared")
    set_kind("shared")
    set_warnings("all")
    add_deps("objs")
    set_basename("radical")
    set_prefixname("lib")

target("headers")
    set_kind("headeronly")
    add_headerfiles("include/(*.h)") 

target("eyyc")
    set_kind("binary")
    add_files("examples/eyyc.c")
    set_default(false)
    set_warnings("all")
    add_deps("radicalite_shared")
    add_rpathdirs(".")
    add_rpathdirs("/usr/lib")
    add_rpathdirs("/usr/local/lib")
    add_rpathdirs("@executable_path")

执行后部分输出如下:

collect2.exe: error: ld returned 1 exit status
error: execv(D:\mingw64\bin\x86_64-w64-mingw32-g++ -o build\windows\x64\debug\eyyc.exe build\.objs\eyyc\windows\x64\debug\examples\eyyc.c.obj -Wl,-rpath=. -Wl,-rpath=\usr\lib -Wl,-rpath=\usr\local\lib -Wl,-rpath=$ORIGIN -g) failed(1)

target("eyyc") 中有 add_deps("radicalite_shared") 继承动态库依赖,然而生成的编译命令中并没有-L-l,linker也不会链接这个库,导致链接失败。

在v2.7.5版本中似乎没有此问题。

期待的结果

linker能够链接通过add_deps依赖的动态库

工程配置

见“描述问题”

附加信息和错误日志

[ 93%]: linking.debug eyyc.exe
D:\mingw64\bin\x86_64-w64-mingw32-g++ -o build\windows\x64\debug\eyyc.exe build\.objs\eyyc\windows\x64\debug\examples\eyyc.c.obj -Wl,-rpath=. -Wl,-rpath=\usr\lib -Wl,-rpath=\usr\local\lib -Wl,-rpath=$ORIGIN -g
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: build\.objs\eyyc\windows\x64\debug\examples\eyyc.c.obj: in function `eyyc':
D:\\*****\\radicalite/examples/eyyc.c:18: undefined reference to `initPoly'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:19: undefined reference to `initPoly'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:22: undefined reference to `initRad'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:23: undefined reference to `Radsqrt'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:23: undefined reference to `addRad'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:23: undefined reference to `addRad'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:25: undefined reference to `_PolyIntReduce'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:26: undefined reference to `printPoly'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:32: undefined reference to `destoryPoly'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:36: undefined reference to `Radsqrt'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:36: undefined reference to `addRad'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:36: undefined reference to `subRad'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:37: undefined reference to `_PolyIntReduce'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:38: undefined reference to `printPoly'
D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\\*****\\radicalite/examples/eyyc.c:44: undefined reference to `destoryPoly'
collect2.exe: error: ld returned 1 exit status
error: @programdir\core\main.lua:329: @programdir\actions\build\main.lua:148: @programdir\modules\async\runjobs.lua:322: @programdir\actions\build\kinds\binary.lua:53: @programdir\core\sandbox\modules\os.lua:378: execv(D:\mingw64\bin\x86_64-w64-mingw32-g++ -o build\windows\x64\debug\eyyc.exe build\.objs\eyyc\windows\x64\debug\examples\eyyc.c.obj -Wl,-rpath=. -Wl,-rpath=\usr\lib -Wl,-rpath=\usr\local\lib -Wl,-rpath=$ORIGIN -g) failed(1)
stack traceback:
    [C]: in function 'error'
    [@programdir\core\base\os.lua:973]:
    [@programdir\core\sandbox\modules\os.lua:378]: in function 'execv'
    [@programdir\modules\core\tools\gcc.lua:559]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:275]:
    [@programdir\core\tool\linker.lua:221]: in function 'link'
    [@programdir\actions\build\kinds\binary.lua:53]: in function 'callback'
    [@programdir\modules\core\project\depend.lua:217]: in function 'on_changed'
    [@programdir\actions\build\kinds\binary.lua:41]: in function '_do_link_target'
    [@programdir\actions\build\kinds\binary.lua:83]:
    [@programdir\actions\build\kinds\binary.lua:110]: in function '_link_target'
    [@programdir\actions\build\kinds\binary.lua:138]: in function 'jobfunc'
    [@programdir\modules\async\runjobs.lua:238]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:275]: in function 'trycall'
    [@programdir\core\sandbox\modules\try.lua:117]: in function 'try'
    [@programdir\modules\async\runjobs.lua:220]: in function 'cotask'
    [@programdir\core\base\scheduler.lua:406]:

stack traceback:
        [C]: in function 'error'
        @programdir\core\base\os.lua:973: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir\core\main.lua:329: in upvalue 'cotask'
        @programdir\core\base\scheduler.lua:406: in function <@programdir\core\base\scheduler.lua:399>

*****是我在输出后手动遮蔽的目录,对问题无影响)

Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically.


Title: add_deps cannot be automatically linked

waruqi commented 4 months ago

贴下完整 -vD logs,并且提供一个可复现的 example project

Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically.


Post the complete -vD logs and provide a reproducible example project

RunThem commented 3 weeks ago

他说的可能是这种情况:

-- xmake.lua
target('foo', function()
  set_kind('object')
  add_files('foo.c')

  before_build(function(T)
    io.writefile(
      'foo.c',
      [[
void foo() {}
]]
    )
  end)
end)

target('shared', function()
  set_kind('shared')

  add_deps('foo')
end)

target('add_deps', function()
  set_kind('binary')
  add_files('main.c')

  add_deps('shared')

  before_build(function(T)
    io.writefile(
      'main.c',
      [[
extern void foo();

int main() {
  foo();
}
]]
    )
  end)
end)
[ 40%]: cache compiling.release foo.c
/usr/bin/gcc -c -m64 -o build/.objs/foo/linux/x86_64/release/foo.c.o foo.c
[ 50%]: cache compiling.release main.c
/usr/bin/gcc -c -m64 -o build/.objs/add_deps/linux/x86_64/release/main.c.o main.c
[ 70%]: linking.release libshared.so
/usr/bin/g++ -o build/linux/x86_64/release/libshared.so build/.objs/foo/linux/x86_64/release/foo.c.o -shared -m64 -fPIC
[ 90%]: linking.release add_deps
/usr/bin/g++ -o build/linux/x86_64/release/add_deps build/.objs/add_deps/linux/x86_64/release/main.c.o -m64 -Wl,-rpath=$ORIGIN
/usr/bin/ld: build/.objs/add_deps/linux/x86_64/release/main.c.o: in function `main':
main.c:(.text+0xa): undefined reference to `foo'
collect2: error: ld returned 1 exit status
error: execv(/usr/bin/g++ -o build/linux/x86_64/release/add_deps build/.objs/add_deps/linux/x86_64/release/main.c.o -m64 -Wl,-rpath=$ORIGIN) failed(1)

链接 add_deps 时没有 -lshared flag.