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

ubuntu 21.10 clang 13环境下编译动态库加-flto编译选项后实际编译时-flto选项没有附加 #2281

Closed
bamlan opened this issue Apr 19, 2022 · 5 comments
Labels

Comments

@bamlan
Copy link

bamlan commented Apr 19, 2022

Xmake 版本

2.6.4

操作系统版本和架构

ubuntu 21.10 clang 13

描述问题

只有动态库有这个问题,看下面的编译信息:
[ 81%]: linking.release qjsc
/usr/bin/clang++ -o build/linux/x86_64/release/qjsc build/.objs/qjsc/linux/x86_64/release/qjsc.c.o -m64 -Lbuild/linux/x86_64/release -s -lquickjs.lto -lm -ldl -lpthread -flto
[ 87%]: linking.release qjs
/usr/bin/clang++ -o build/linux/x86_64/release/qjs build/.objs/qjs/linux/x86_64/release/qjs.c.o build/.objs/qjs/linux/x86_64/release/repl.c.o build/.objs/qjs/linux/x86_64/release/qjscalc.c.o -m64 -Lbuild/linux/x86_64/release -s -lquickjs.lto -lm -ldl -lpthread -flto -rdynamic
checking for clang++ ... /usr/bin/clang++
checking for the shared library linker (sh) ... clang++
checking for flags (-fPIC) ... ok
[ 93%]: linking.release curljs.so
/usr/bin/clang++ -o build/linux/x86_64/release/curljs.so build/.objs/curljs/linux/x86_64/release/qjs_cmod/curljs.c.o -shared -fPIC -m64 -L/usr/lib/x86_64-linux-gnu -Lbuild/linux/x86_64/release -s -lrt -lcrypto -lssl -lcurl -lquickjs.lto -lm -ldl -lpthread
error: build/.objs/curljs/linux/x86_64/release/qjs_cmod/curljs.c.o: file not recognized: file format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)

手动加上-flto编译成功
xxxx@xxxx:/codes/js/quickjs/nqjspp$ /usr/bin/clang++ -o build/linux/x86_64/release/curljs.so build/.objs/curljs/linux/x86_64/release/qjs_cmod/curljs.c.o -shared -fPIC -m64 -L/usr/lib/x86_64-linux-gnu -Lbuild/linux/x86_64/release -s -lrt -lcrypto -lssl -lcurl -lquickjs.lto -lm -ldl -lpthread -flto

在xmake.lua中已经加上了:
add_cflags("-flto")
add_cxxflags("-flto")

在set_kind("shared")的target中,-flto编译时消失了。

期待的结果

编译动态库时-flto编译选项能正常附加

工程配置

No response

附加信息和错误日志

No response

@bamlan bamlan added the bug label Apr 19, 2022
@waruqi
Copy link
Member

waruqi commented Apr 19, 2022

cflags/cxxflags 是编译 flags 不是 linker flags,你只加了编译 flags,却在 linker flags 里找,怎么可能找到

shared 用 add_shflags, binary 用 add_ldflags

@bamlan
Copy link
Author

bamlan commented Apr 19, 2022

谢谢,我看了xmake.io的文档加上了。这里还想问下,target B的源文件是target A编译后生成的,在target B中使用add_files添加了文件名,并add_deps了target A,在第一次使用xmake时,会报源文件未找到的错误,当然重新再编译一次就能OK, 有没有什么办法能一次编译成功么? 我在target B里使用before_link里调用target:add("files", "..."), 文件能加上,但没有生成.o文件。

@waruqi
Copy link
Member

waruqi commented Apr 19, 2022

#1090 (comment)

@bamlan
Copy link
Author

bamlan commented Apr 19, 2022

谢谢你的回复,我看了下#1090, 好像也不能解决我提的这种情况,我用xmake创建了一个简单的project,你有空的时候帮忙看下:
bwaita.zip

xmake.lua如下:

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

add_cflags("-fPIC")
add_cxxflags("-fPIC")

target("a")
    set_kind("binary")
    add_files("src/a.cpp")

    -- 链接完成后
    after_link(function (target)
        --生成项目B引用的wait.c文件
        os.run(target:targetfile() .. " $(scriptdir)/src/wait.cpp")
    end)

target("b")
    set_kind("binary")
    add_deps("a")
    add_files("src/b.cpp")
    --[[添加 {always_added = true} 直接编译报错:

    [ 22%]: ccache compiling.release src/b.cpp
    [ 22%]: ccache compiling.release src/a.cpp
    [ 22%]: ccache compiling.release src/wait.cpp
    error: cc1plus: fatal error: src/wait.cpp: 没有那个文件或目录
    compilation terminated.
    > in src/wait.cpp

    不添加 {always_added = true} 第一次执行xmake:
    [ 25%]: ccache compiling.release src/a.cpp
    [ 25%]: ccache compiling.release src/b.cpp
    [ 50%]: linking.release a
    [ 75%]: linking.release b
    error: /usr/bin/ld: build/.objs/b/linux/x86_64/release/src/b.cpp.o: in function `main':
    b.cpp:(.text.startup+0x11): undefined reference to `wait(int)'
    collect2: error: ld returned 1 exit status

    warning: cannot match target(b).add_files("src/wait.cpp") at ./xmake.lua:31

    再执行一次xmake可编译成功
    --]]
    add_files("src/wait.cpp"--[[, {always_added = true}--]])

@waruqi
Copy link
Member

waruqi commented Apr 19, 2022

不同的问题 不要混一起,单开个 issues

@waruqi waruqi closed this as completed Apr 19, 2022
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

2 participants