xmake-io / xmake

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

linux下某些包引用后导致lib链接丢失 #4307

Closed sinnren closed 11 months ago

sinnren commented 11 months ago

Xmake 版本

xmake v2.8.2+20230914

操作系统版本和架构

wsl ubuntu20

描述问题

一个target ` add_rules("mode.debug", "mode.release")

add_requires("openssl", "drogon")

target("sqlcipher") set_kind("static") add_defines("SQLITE_HAS_CODEC=1", {public = true}) add_includedirs("include", {public = true}) add_files("src/sqlite3.c") add_packages("openssl")

if is_plat("linux") then
    add_syslinks("m", "dl", "pthread")
end

target("sqlcipher_shell") set_kind("binary") add_deps("sqlcipher") add_files("src/shell.c")

target("error_test") set_kind("binary") add_deps("sqlcipher") add_packages("drogon") add_files("test.cc") `

xmake build sqlcipher_shell,sqlcipher_shell工程可以正常生成。

error_test项目里面引用了drogon这个包,引用后再编译: xmake build error_test 就会报链openssl的链接错误 [ 75%]: linking.release error_test error: /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in functionsqlcipher_openssl_get_cipher': sqlite3.c:(.text+0x5911): undefined reference to EVP_CIPHER_nid' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in functionsqlcipher_openssl_get_block_sz': sqlite3.c:(.text+0x58b5): undefined reference to EVP_CIPHER_block_size' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in functionsqlcipher_openssl_get_iv_sz': sqlite3.c:(.text+0x58d5): undefined reference to EVP_CIPHER_iv_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in functionsqlcipher_openssl_get_key_sz': sqlite3.c:(.text+0x58f5): undefined reference to EVP_CIPHER_key_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in functionsqlcipher_openssl_get_hmac_sz': sqlite3.c:(.text+0x7c4d): undefined reference to EVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c65): undefined reference toEVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c7d): undefined reference to EVP_MD_size' collect2: error: ld returned 1 exit status

提示openssl的几个方法链接不到了,这是什么原因?疑似就是linux下drogon库不知道什么设置导致了openssl链接失败。。。

期待的结果

能正确检测并链接生成目标文件。

工程配置

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

add_requires("openssl", "drogon")

target("sqlcipher") set_kind("static") add_defines("SQLITE_HAS_CODEC=1", {public = true}) add_includedirs("include", {public = true}) add_files("src/sqlite3.c") add_packages("openssl")

if is_plat("linux") then
    add_syslinks("m", "dl", "pthread")
end

target("sqlcipher_shell") set_kind("binary") add_deps("sqlcipher") add_files("src/shell.c")

target("error_test") set_kind("binary") add_deps("sqlcipher") add_packages("drogon") add_files("test.cc")

附加信息和错误日志

[ 75%]: linking.release error_test /usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/test.cc.o -m64 -L/home/fenlog/vcpkg/installed/x64-linux/lib -L/home/fenlog/.xmake/packages/t/trantor/v1.5.8/e6e6ae689ff74a9dbe8db2caf5c822d1/lib -L/home/fenlog/.xmake/packages/c/c-ares/1.19.0/de08b8aebbb8402582b5466949d39ab8/lib -L/home/fenlog/.xmake/packages/j/jsoncpp/1.9.5/6a59121558c24c01a73b20670010be3a/lib -L/home/fenlog/.xmake/packages/b/brotli/v1.1.0/e3f40d36e48b41ef850d98dfb7dfc2cd/lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1-t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -lz -luuid -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_cipher': sqlite3.c:(.text+0x5911): undefined reference toEVP_CIPHER_nid' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_block_sz': sqlite3.c:(.text+0x58b5): undefined reference toEVP_CIPHER_block_size' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_iv_sz': sqlite3.c:(.text+0x58d5): undefined reference toEVP_CIPHER_iv_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_key_sz': sqlite3.c:(.text+0x58f5): undefined reference toEVP_CIPHER_key_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_hmac_sz': sqlite3.c:(.text+0x7c4d): undefined reference toEVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c65): undefined reference to EVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c7d): undefined reference toEVP_MD_size' collect2: error: ld returned 1 exit status error: @programdir/modules/private/async/runjobs.lua:256: @programdir/actions/build/kinds/binary.lua:74: @programdir/core/sandbox/modules/os.lua:378: execv(/usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/test.cc.o -m64 -L/home/fenlog/vcpkg/installed/x64-linux/lib -L/home/fenlog/.xmake/packages/t/trantor/v1.5.8/e6e6ae689ff74a9dbe8db2caf5c822d1/lib -L/home/fenlog/.xmake/packages/c/c-ares/1.19.0/de08b8aebbb8402582b5466949d39ab8/lib -L/home/fenlog/.xmake/packages/j/jsoncpp/1.9.5/6a59121558c24c01a73b20670010be3a/lib -L/home/fenlog/.xmake/packages/b/brotli/v1.1.0/e3f40d36e48b41ef850d98dfb7dfc2cd/lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1-t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -lz -luuid -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl) failed(1) stack traceback: [C]: in function 'error' [@programdir/core/base/os.lua:921]: [@programdir/core/sandbox/modules/os.lua:378]: in function 'execv' [@programdir/modules/core/tools/gcc.lua:483]: [C]: in function 'xpcall' [@programdir/core/base/utils.lua:280]: [@programdir/core/tool/linker.lua:224]: in function 'link' [@programdir/actions/build/kinds/binary.lua:74]: in function 'callback' [@programdir/modules/core/project/depend.lua:193]: in function 'on_changed' [@programdir/actions/build/kinds/binary.lua:55]: in function '_do_link_target' [@programdir/actions/build/kinds/binary.lua:102]: [@programdir/actions/build/kinds/binary.lua:129]: in function '_link_target' [@programdir/actions/build/kinds/binary.lua:157]: in function 'jobfunc' [@programdir/modules/private/async/runjobs.lua:232]: [C]: in function 'xpcall' [@programdir/core/base/utils.lua:280]: in function 'trycall' [@programdir/core/sandbox/modules/try.lua:117]: in function 'try' [@programdir/modules/private/async/runjobs.lua:218]: in function 'cotask' [@programdir/core/base/scheduler.lua:404]:

stack traceback: [C]: in function 'error' @programdir/core/base/os.lua:921: in function 'os.raiselevel' (...tail calls...) @programdir/modules/private/async/runjobs.lua:256: in field 'catch' @programdir/core/sandbox/modules/try.lua:123: in global 'try' @programdir/modules/private/async/runjobs.lua:218: in upvalue 'cotask' @programdir/core/base/scheduler.lua:404: in function <@programdir/core/base/scheduler.lua:397>

Issues-translate-bot commented 11 months ago

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


Title: Some packages under Linux cause the lib link to be lost after being referenced

waruqi commented 11 months ago

add_packages("openssl") 你仅仅只是设置到了 sqlcipher 这个 static target,那么仅仅这个 target 内部依赖 openssl,并不会对 error_test 生效,要想 error_test target 也要 link 它,得设置 public 导出到其他 target 可见。add_packages("openssl", {public = true})

sinnren commented 11 months ago

add_packages("openssl") 你仅仅只是设置到了 sqlcipher 这个 static target,那么仅仅这个 target 内部依赖 openssl,并不会对 error_test 生效,要想 error_test target 也要 link 它,得设置 public 导出到其他 target 可见。add_packages("openssl", {public = true})

不是不是,跟pulic没有关系。看这个例子:

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

add_requires("openssl", "drogon")

target("sqlcipher") set_kind("static") add_defines("SQLITE_HAS_CODEC=1", {public = true}) add_includedirs("include", {public = true}) add_files("src/sqlite3.c") add_packages("openssl", {public = true})

if is_plat("linux") then
    add_syslinks("m", "dl", "pthread")
end

target("sqlcipher_shell") set_kind("binary") add_deps("sqlcipher") add_files("src/shell.c")

target("error_test") set_kind("binary") add_deps("sqlcipher") add_packages("drogon") add_files("src/shell.c")

你看sqlcipher_shell和error_test这两个项目,区别仅仅是error_test这个项目里面,额外引用了drogon这个库(xmake-repo里面的)。但是sqlcipher_shell正常编译,error_test无法编译报了链接错误。 [ 66%]: linking.release error_test error: /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_cipher': sqlite3.c:(.text+0x5911): undefined reference toEVP_CIPHER_nid' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_block_sz': sqlite3.c:(.text+0x58b5): undefined reference toEVP_CIPHER_block_size' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_iv_sz': sqlite3.c:(.text+0x58d5): undefined reference toEVP_CIPHER_iv_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_key_sz': sqlite3.c:(.text+0x58f5): undefined reference toEVP_CIPHER_key_length' /usr/bin/ld: build/linux/x86_64/release/libsqlcipher.a(sqlite3.c.o): in function sqlcipher_openssl_get_hmac_sz': sqlite3.c:(.text+0x7c4d): undefined reference toEVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c65): undefined reference to EVP_MD_size' /usr/bin/ld: sqlite3.c:(.text+0x7c7d): undefined reference toEVP_MD_size' collect2: error: ld returned 1 exit status

waruqi commented 11 months ago

那就 -v 自己看下 links flags 顺序

Issues-translate-bot commented 11 months ago

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


Then use -v to see for yourself the order of links flags

sinnren commented 11 months ago

去掉drogon库的引用,链接命令是: /usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/src/shell.c.o -m64 -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1-t/25a06d63c5c94540b3812b3f969192cb/lib -s -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl 正常编译。

引用了drogon库后,链接命令: /usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/src/shell.c.o -m64 -L/home/fenlog/vcpkg/installed/x64-linux/lib -L/home/fenlog/.xmake/packages/c/c-ares/1.19.0/de08b8aebbb8402582b5466949d39ab8/lib -L/home/fenlog/.xmake/packages/j/jsoncpp/1.9.5/6a59121558c24c01a73b20670010be3a/lib -L/home/fenlog/.xmake/packages/b/brotli/v1.1.0/e3f40d36e48b41ef850d98dfb7dfc2cd/lib -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1-t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl 报了openssl的链接异常

不知道问题出在哪里。。。

Issues-translate-bot commented 11 months ago

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


Remove the reference to the drogon library. The link command is: /usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/src/shell.c.o -m64 -Lbuild/linux/x86_64/release -L/home/ fenlog/.xmake/packages/o/openssl/1.1.1-t/25a06d63c5c94540b3812b3f969192cb/lib -s -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl Compiles normally.

After referencing the drogon library, link the command: /usr/bin/g++ -o build/linux/x86_64/release/error_test build/.objs/error_test/linux/x86_64/release/src/shell.c.o -m64 -L/home/fenlog/vcpkg/installed/x64- linux/lib -L/home/fenlog/.xmake/packages/c/c-ares/1.19.0/de08b8aebbb8402582b5466949d39ab8/lib -L/home/fenlog/.xmake/packages/j/jsoncpp/1.9.5/6a59121558c24c01a73b20670010be3 a/ lib -L/home/fenlog/.xmake/packages/b/brotli/v1.1.0/e3f40d36e48b41ef850d98dfb7dfc2cd/lib -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl Reported openssl link exception

Don't know where the problem lies. . .

waruqi commented 11 months ago

-L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl

看 link 顺序 和 flags 没啥问题,-lsqlcipher -lssl -lcrypto 这是 ok 的。。

唯一的区别就是前面多了个 -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib. vcpkg 的 lib 目录下所有库 是不分子目录的,可能 vcpkg 的 lib 里,也有一份 ssl 库,并且里面符号不完整,导致 link 时候,优先从 vcpkg 的 lib 里面去找 ssl 使用了。。没用到 /home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib 里面的。。

可以试着去掉 vcpkg 的库,不要混用试试。或者 vcpkg 里面把 ssl 卸载了

Issues-translate-bot commented 11 months ago

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


-L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon - luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl

There is nothing wrong with the link order and flags. -lsqlcipher -lssl -lcrypto is ok. .

The only difference is that there is an additional -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib. All libraries in the lib directory of vcpkg do not have subdirectories. Maybe there is an ssl library in vcpkg's lib, and the symbols in it are incomplete. As a result, when linking, SSL is first used in vcpkg's lib. . The ones in /home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib are not used. .

You can try to remove the vcpkg library instead of mixing it. Or uninstall ssl in vcpkg

sinnren commented 11 months ago

-L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl

看 link 顺序 和 flags 没啥问题,-lsqlcipher -lssl -lcrypto 这是 ok 的。。

唯一的区别就是前面多了个 -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib. vcpkg 的 lib 目录下所有库 是不分子目录的,可能 vcpkg 的 lib 里,也有一份 ssl 库,并且里面符号不完整,导致 link 时候,优先从 vcpkg 的 lib 里面去找 ssl 使用了。。没用到 /home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib 里面的。。

可以试着去掉 vcpkg 的库,不要混用试试。或者 vcpkg 里面把 ssl 卸载了

卧槽,大佬牛逼!!~~~搞定!

Issues-translate-bot commented 11 months ago

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


-L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib -L/home/fenlog/.xmake/packages/z/zlib/v1.3/45ea3fb9069b468faf0ef6782964c796/ lib -Lbuild/linux/x86_64/release -L/home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib -s -ldrogon -ltrantor -lcares -ljsoncpp -lbrotlienc -lbrotlidec -lbrotlicommon -luuid -lz -lsqlcipher -lssl -lcrypto -lm -lpthread -ldl

There is nothing wrong with the link order and flags. -lsqlcipher -lssl -lcrypto is ok. .

The only difference is that there is an additional -L/home/fenlog/vcpkg/installed/x64-linux/lib/pkgconfig/../../lib. All libraries in the lib directory of vcpkg are not sub-directories. , maybe there is an SSL library in the lib of vcpkg, and the symbols in it are incomplete, so when linking, SSL is used first from the lib of vcpkg. . The ones in /home/fenlog/.xmake/packages/o/openssl/1.1.1 -t/25a06d63c5c94540b3812b3f969192cb/lib are not used. .

You can try to remove the vcpkg library instead of mixing it. Or uninstall ssl in vcpkg

Damn it, the boss is awesome! ! ~~~ Done!