Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_deps不能自动链接 #5046

Open
maxchen32 opened this issue May 3, 2024 · 4 comments
Open

add_deps不能自动链接 #5046

maxchen32 opened this issue May 3, 2024 · 4 comments
Labels

Comments

@maxchen32
Copy link

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>

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

@maxchen32 maxchen32 added the bug label May 3, 2024
@Issues-translate-bot
Copy link

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


Title: add_deps cannot be automatically linked

@waruqi
Copy link
Member

waruqi commented May 4, 2024

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

@Issues-translate-bot
Copy link

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
Copy link

RunThem commented Aug 22, 2024

他说的可能是这种情况:

-- 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants