# How to get object file to link first?

**URL:** <https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [February 9, 2021, 4:12pm UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729 "2021-02-09T16:12:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mats\_Liljegren](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mats_liljegren/32/1192_2.png) [@Mats\_Liljegren](https://discourse.cmake.org/u/Mats_Liljegren)\
**Post date:** [February 9, 2021, 4:12pm UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729/1 "2021-02-09T16:12:34Z")

</div>

I’m trying to compile a Windows DLL for code originally intended for a Unix platform. One problem I have is that Windows `fopen()` defaults to `O_TEXT`, but Unix defaults to `O_BINARY`.

Windows recommends two ways to solve this:

1. Use `_set_fmode()` function, didn’t work for me probably because I could not call it from correct context.
2. Link with `binmode.obj` object file. This file is included with the compiler, and the compiler knows where to look for it.

The latter worked for my own code, but it does not seem to work for third party static libraries. Looking at the linker output, I can see that static libraries are included before `binmode.obj`, but `binmode.obj` was included before my own object files.

The real solution should be to persuade all external libraries to include `binmode.obj`, but this is somewhat non-trivial and might incur significant maintenance overhead in the future.

So I’d like to try to move the `binmode.obj` to the beginning of the linker command line, but this proved to be surprisingly difficult. I’ve tried:

1. `target_link_libraries( mylib PRIVATE binmode.obj )` - Got correct position, but got renamed to `binmode.obj.lib`.
2. Tried making a static library with `binmode.obj` as a pre-compiled object, but got an error that the file has a relative path. I have no idea how to make it a full path.
3. Tried adding it as a linker flag in different ways, even including using toolchain file, but it all ended up with `binmode.obj` being placed after libraries.

I’m now out of ideas and feel I need help with this. Any suggestions how to fix this?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [February 10, 2021, 6:46pm UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729/2 "2021-02-10T18:46:35Z")

</div>

What if you do `target_sources(mylib PRIVATE binmode.obj)`?

---

<div class="post-metadata">

**Author:** ![Mats\_Liljegren](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mats_liljegren/32/1192_2.png) [@Mats\_Liljegren](https://discourse.cmake.org/u/Mats_Liljegren)\
**Post date:** [February 11, 2021, 11:54am UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729/3 "2021-02-11T11:54:00Z")

</div>

That would require a full path to binmode.obj. However, if I did an UNKNOWN IMPORTED GLOBAL library. I then used IMPORTED\_LOCATION property set binmode.obj. This worked like a charm. Just a shame it didn’t solve my original problem, but that has nothing to do with CMake…

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [February 11, 2021, 1:34pm UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729/4 "2021-02-11T13:34:06Z")

</div>

Ah, `binmode.obj` is provided by the platform and not your project? I don’t know if there’s a way to control the link line order. @brad.king?

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [February 11, 2021, 1:46pm UTC](https://discourse.cmake.org/t/how-to-get-object-file-to-link-first/2729/5 "2021-02-11T13:46:20Z")

</div>

Try using `target_link_options(mylib PRIVATE binmode.obj)`.

Or, to apply to all shared library targets, put it in `CMAKE_SHARED_LINKER_FLAGS`.
