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

[Question]: How to maintain order of libraries #409

Closed
hoehermann opened this issue Jun 22, 2023 · 2 comments
Closed

[Question]: How to maintain order of libraries #409

hoehermann opened this issue Jun 22, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@hoehermann
Copy link

hoehermann commented Jun 22, 2023

Current Behavior

Using

target_link_libraries(main PRIVATE rust_staticlib m)

results in

/usr/bin/cc -g "main.c.o" -o main  -lm librust_staticlib.a 

leading to

/build/rustc-Oic09u/rustc-1.65.0+dfsg0ubuntu1/library/std/src/f64.rs:88: undefined reference to `round'

.

Expected Behavior

Using

target_link_libraries(main PRIVATE rust_staticlib m)

results in

/usr/bin/cc -g "main.c.o" -o main librust_staticlib.a -lm

leading to successful build.

Steps To Reproduce

https://github.com/hoehermann/purple-presage in I am trying to link a rust staticlib into a shared object. The staticlib seems to need some math functions, so I added m to the target_link_libraries stanza:

add_executable(main "src/c/main.c")
target_link_libraries(main PRIVATE rust_staticlib m)

Am I using corrosion wrong? Or is this a common CMake issue I have not encountered so far?

This may or may not be related to #335.

I added the verbose outputs as requested. I hope it helps in spite of the libpurple stuff adding a lot of clutter.

Environment

- OS: Ubuntu 22.04
- CMake: 3.24.0
- CMake Generator: Unix Makefiles

CMake configure log with Debug log-level

-- PURPLE_INCLUDE_DIRS: /usr/include/libpurple;/usr/include/glib-2.0;/usr/lib/x86_64-linux-gnu/glib-2.0/include
-- PURPLE_LIBRARY_DIRS: /usr/lib;/usr/lib/x86_64-linux-gnu
-- PURPLE_LIBRARIES: purple;glib-2.0
-- Rust Target: x86_64-unknown-linux-gnu
-- Parsed Target triple: arch: x86_64, vendor: unknown, OS: linux, env: gnu
-- Parsed Target triple: arch: x86_64, vendor: unknown, OS: linux, env: gnu
-- Using Corrosion as a subdirectory
-- Found 1 targets in package purple-presage-backend
-- TARGET purple_presage_backend produces byproducts libpurple_presage_backend.a;;
-- Corrosion created the following CMake targets: purple_presage_backend
-- PURPLE_PLUGIN_DIR: /usr/lib/purple-2
-- Output directory property (target purple_presage_backend): ARCHIVE_OUTPUT_DIRECTORY dir: output_directory-NOTFOUND
-- Setting IMPORTED_LOCATION for target purple_presage_backend-static to `REDACTED/build/libpurple_presage_backend.a`.
-- Adding command to copy byproducts `libpurple_presage_backend.a` to REDACTED/build/libpurple_presage_backend.a
-- Configuring done
-- Generating done
-- Build files have been written to: REDACTED/build

CMake Build step log

/usr/bin/cmake -SREDACTED -BREDACTED/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start REDACTED/build/CMakeFiles REDACTED/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Verzeichnis „REDACTED/build“ wird betreten
make  -f CMakeFiles/cargo-prebuild_purple_presage_backend.dir/build.make CMakeFiles/cargo-prebuild_purple_presage_backend.dir/depend
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" REDACTED REDACTED REDACTED/build REDACTED/build REDACTED/build/CMakeFiles/cargo-prebuild_purple_presage_backend.dir/DependInfo.cmake --color=
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make  -f CMakeFiles/cargo-prebuild_purple_presage_backend.dir/build.make CMakeFiles/cargo-prebuild_purple_presage_backend.dir/build
make[2]: Verzeichnis „REDACTED/build“ wird betreten
make[2]: Für das Ziel „CMakeFiles/cargo-prebuild_purple_presage_backend.dir/build“ ist nichts zu tun.
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
[  0%] Built target cargo-prebuild_purple_presage_backend
make  -f CMakeFiles/_cargo-build_purple_presage_backend.dir/build.make CMakeFiles/_cargo-build_purple_presage_backend.dir/depend
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" REDACTED REDACTED REDACTED/build REDACTED/build REDACTED/build/CMakeFiles/_cargo-build_purple_presage_backend.dir/DependInfo.cmake --color=
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make  -f CMakeFiles/_cargo-build_purple_presage_backend.dir/build.make CMakeFiles/_cargo-build_purple_presage_backend.dir/build
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/src/rust && /usr/bin/cmake -E env CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/cc CC_x86_64-unknown-linux-gnu=/usr/bin/cc CXX_x86_64-unknown-linux-gnu=/usr/bin/c++ CORROSION_BUILD_DIR=REDACTED/build CARGO_BUILD_RUSTC=/usr/bin/rustc /usr/bin/cargo rustc --lib --target=x86_64-unknown-linux-gnu --package purple-presage-backend --manifest-path REDACTED/src/rust/Cargo.toml --target-dir REDACTED/build/./cargo/build -- -Cdefault-linker-libraries=yes
�[0m�[0m�[1m�[32m    Finished�[0m dev [unoptimized + debuginfo] target(s) in 0.23s
�[34m�[1mCopying byproducts `libpurple_presage_backend.a` to REDACTED/build�[0m
/usr/bin/cmake -E make_directory REDACTED/build
/usr/bin/cmake -E copy_if_different REDACTED/build/./cargo/build/x86_64-unknown-linux-gnu/debug/libpurple_presage_backend.a REDACTED/build
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
[  0%] Built target _cargo-build_purple_presage_backend
make  -f CMakeFiles/cargo-build_purple_presage_backend.dir/build.make CMakeFiles/cargo-build_purple_presage_backend.dir/depend
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" REDACTED REDACTED REDACTED/build REDACTED/build REDACTED/build/CMakeFiles/cargo-build_purple_presage_backend.dir/DependInfo.cmake --color=
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make  -f CMakeFiles/cargo-build_purple_presage_backend.dir/build.make CMakeFiles/cargo-build_purple_presage_backend.dir/build
make[2]: Verzeichnis „REDACTED/build“ wird betreten
make[2]: Für das Ziel „CMakeFiles/cargo-build_purple_presage_backend.dir/build“ ist nichts zu tun.
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
[  0%] Built target cargo-build_purple_presage_backend
make  -f CMakeFiles/purple-presage.dir/build.make CMakeFiles/purple-presage.dir/depend
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" REDACTED REDACTED REDACTED/build REDACTED/build REDACTED/build/CMakeFiles/purple-presage.dir/DependInfo.cmake --color=
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make  -f CMakeFiles/purple-presage.dir/build.make CMakeFiles/purple-presage.dir/build
make[2]: Verzeichnis „REDACTED/build“ wird betreten
make[2]: Für das Ziel „CMakeFiles/purple-presage.dir/build“ ist nichts zu tun.
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
[ 50%] Built target purple-presage
make  -f CMakeFiles/test-presage.dir/build.make CMakeFiles/test-presage.dir/depend
make[2]: Verzeichnis „REDACTED/build“ wird betreten
cd REDACTED/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" REDACTED REDACTED REDACTED/build REDACTED/build REDACTED/build/CMakeFiles/test-presage.dir/DependInfo.cmake --color=
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make  -f CMakeFiles/test-presage.dir/build.make CMakeFiles/test-presage.dir/build
make[2]: Verzeichnis „REDACTED/build“ wird betreten
[ 75%] �[32m�[1mLinking C executable test-presage�[0m
/usr/bin/cmake -E cmake_link_script CMakeFiles/test-presage.dir/link.txt --verbose=1
/usr/bin/cc -g "CMakeFiles/test-presage.dir/src/c/main.c.o" -o test-presage  -lm libpurple_presage_backend.a 
/usr/bin/ld: libpurple_presage_backend.a(sled-6db45d69e2a9ad3e.sled.09113f11-cgu.5.rcgu.o): in function `std::f64::<impl f64>::round':
/build/rustc-Oic09u/rustc-1.65.0+dfsg0ubuntu1/library/std/src/f64.rs:88: undefined reference to `round'
/usr/bin/ld: libpurple_presage_backend.a(rand-7f1ebc9c3a94bcda.rand.29cfc123-cgu.14.rcgu.o): in function `std::f64::<impl f64>::ln::{{closure}}':
/build/rustc-Oic09u/rustc-1.65.0+dfsg0ubuntu1/library/std/src/f64.rs:427: undefined reference to `log'
/usr/bin/ld: libpurple_presage_backend.a(rand-7f1ebc9c3a94bcda.rand.29cfc123-cgu.14.rcgu.o): in function `std::f64::<impl f64>::exp':
/build/rustc-Oic09u/rustc-1.65.0+dfsg0ubuntu1/library/std/src/f64.rs:384: undefined reference to `exp'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test-presage.dir/build.make:98: test-presage] Fehler 1
make[2]: Verzeichnis „REDACTED/build“ wird verlassen
make[1]: *** [CMakeFiles/Makefile2:291: CMakeFiles/test-presage.dir/all] Fehler 2
make[1]: Verzeichnis „REDACTED/build“ wird verlassen
make: *** [Makefile:136: all] Fehler 2
�[?2004h�]0;hermann@i5: REDACTED/build��[01;32mhermann@i5�[00m:�[01;34mREDACTED/build�[00m$ �[?2004l
@hoehermann hoehermann added the bug Something isn't working label Jun 22, 2023
@jschwe
Copy link
Collaborator

jschwe commented Jun 23, 2023

Which version of corrosion are you using?

Edit: to expand a bit. The actual issue here in my opinion is that corrosion doesn't link in libm automatically for you. Unless you turned off the option, e.g. by passing NO_STD to corrosion_import_crate, I would like to know which version of corrosion this occured on.

In response to the question you asked:

As far as I know CMake doesn't guarantee a particular order for the libraries for a specific target and is free to reorder. The issue you are facing is because you are declaring libm to be a dependency of main, when it actually is a dependency of your rust static lib. As such you should do target_link_libraries(your_rust_lib INTERFACE c_lib).
Cmake is aware of the dependency in this case and will order them accordingly.

But again, for libm corrosion should do this automatically.

@hoehermann
Copy link
Author

I did not specify and version. FetchContent defaulted to corrosion 0.4.1 at 4a134df.

target_link_libraries(your_rust_lib INTERFACE c_lib) solves the issue. So I was using cmake wrong. Thank you very much.

@jschwe jschwe closed this as completed Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants