# How to link .o files generated by rustc?

**URL:** <https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434>\
**Category:** help\
**Created:** [October 30, 2024, 9:45am UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434 "2024-10-30T09:45:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wallby](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Wallby](https://users.rust-lang.org/u/Wallby)\
**Post date:** [October 30, 2024, 9:45am UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/1 "2024-10-30T09:45:32Z")

</div>

So I saw the rustc compiler has the option to generate .o files, by specifying `--emit obj`. I tried using CMake to do the following..

```rust
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Test.rs.o COMMAND rustc -o ${CMAKE_CURRENT_BINARY_DIR}/Test.rs.o --crate-type staticlib --emit obj ${CMAKE_CURRENT_SOURCE_DIR}/Source/Test.rs)
add_custom_target(Test.rs.o DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Test.rs.o)

add_executable(CTest Source/Test.c)
target_link_libraries(CTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/Test.rs.o)
add_dependencies(CTest Test.rs.o)

```

This works but only without any `use` in the Test.rs file. As soon as I add for example `use std::ffi::CString;` a whole bunch of `undefined reference` errors are output by ld (for linking CTest).

---

<div class="post-metadata">

**Author:** ![jendrikw](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jendrikw](https://users.rust-lang.org/u/jendrikw)\
**Post date:** [October 30, 2024, 11:21am UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/2 "2024-10-30T11:21:00Z")

</div>

Please show the error message

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [October 30, 2024, 9:02pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/3 "2024-10-30T21:02:52Z")

</div>

You should not ever try to manually link object files emitted using `--emit obj`. Instead use `--crate-type staticlib` without `--emit obj` and link the `.a` static library that is generated. This bundles all rust dependencies your crate has.

---

<div class="post-metadata">

**Author:** ![Wallby](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Wallby](https://users.rust-lang.org/u/Wallby)\
**Post date:** [November 17, 2024, 12:28pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/4 "2024-11-17T12:28:27Z")

</div>

You mean the .o files cannot be combined with .o files from another language (e.g. C or C++) and linked into one static library?

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [November 17, 2024, 1:13pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/5 "2024-11-17T13:13:58Z")

</div>

If you want a single static library, tell rustc to build a static library and then add the other object files to this static library. Or put the other object files in another static library and then tell rustc to bundle the contents of the former static library (this is the default when linking against static libraries).

---

<div class="post-metadata">

**Author:** ![Wallby](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Wallby](https://users.rust-lang.org/u/Wallby)\
**Post date:** [November 18, 2024, 2:51pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/6 "2024-11-18T14:51:41Z")

</div>

Right that would work for Linux libraries or MinGW libraries (using `ar`), but is that possible for MSVC libraries?

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [November 18, 2024, 3:08pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/7 "2024-11-18T15:08:41Z")

</div>

llvm-ar should handle Windows static libraries produced by both MinGW and MSVC just fine.

---

<div class="post-metadata">

**Author:** ![Wallby](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Wallby](https://users.rust-lang.org/u/Wallby)\
**Post date:** [November 18, 2024, 3:11pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/8 "2024-11-18T15:11:28Z")

</div>

Is there no native MSVC solution? I've never used LLVM.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [November 18, 2024, 3:15pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/9 "2024-11-18T15:15:06Z")

</div>

[Managing a Library | Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/reference/managing-a-library?view=msvc-170) suggest that something like `lib.exe /out:new_library.lib the_rustc_library.lib a.obj b.obj` will create a library `new_library.lib` which has the contents of `the_rustc_library.lib` and also `a.obj` and `b.obj` added.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 16, 2025, 3:16pm UTC](https://users.rust-lang.org/t/how-to-link-o-files-generated-by-rustc/120434/10 "2025-02-16T15:16:02Z")

</div>

Closed on February 16, 2025, 3:16pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
