3

CMake's global property, FIND_LIBRARY_USE_LIB64_PATHS, has the following documentation:

FIND_LIBRARY_USE_LIB64_PATHS is a boolean specifying whether the FIND_LIBRARY command should automatically search the lib64 variant of directories called lib in the search path when building 64-bit binaries.

Reading "when building 64-bit binaries" implies CMake somehow knows my target architecture, and automatically toggles the behavior on/off depending. Am I just reading too far into this, or does CMake have a higher-level abstraction for dealing with 32-bit/64-bit compilation?

If there is, how do I configure whatever mechanism is used by FIND_LIBRARY_USE_LIB64_PATHS, to force a 32-bit/64-bit compiliation?


I realize there are existing questions dealing with forcing 32-bit/64-bit, but they deal with CMAKE_C_FLAGS and the like. If CMake has a higher level abstraction, I'd prefer it to messing with CFLAGS.

midrare
  • 2,371
  • 28
  • 48
  • If you are not cross-compiling, CMake determines the current platform (by compiling some code) and then falls back to all defaults set for that platform. I have never set up a cross compilation process with CMake, so I can't help you in detail here, but if you might wanna check out this doc page: http://www.cmake.org/cmake/help/v3.2/manual/cmake-toolchains.7.html#cross-compiling – Manuzor May 03 '15 at 07:16
  • @sorbet The 32bit-64bit compilation is decided when you choose the compiler, and CMake adapts to that. I cannot imagine any reason for wanting to hack that. – Antonio May 03 '15 at 22:11
  • 1
    @Antonio there kind of is if you want to be able to build both a 32bit and a 64bit version of the output. I've struggled with creating 32 and 64-bit libraries, with different names of course, but have not found any support in CMake itself. Probably have to set a custom variable which can then be tested all over the place... Messy... – thoni56 Apr 12 '17 at 08:41
  • @thoni56 You have to build your project in 2 different build directory. You can use cmake to configure which compiler to use or which compiler settings to use in the 2 different directories. Related: http://stackoverflow.com/q/1474243/2436175 – Antonio Apr 12 '17 at 08:47
  • @Antonio, I know that, but since CMake does not know about '-m32' as a compiler flag or wether your compiler is a cross compiler it will generate libraries with the same name which will install in the same place. I, and the OP, I think, would like CMake to know that a build is a 32-bit build on a 64-bit system so that it can install the 32-bit library where that should go and not overwrite the (usually) /usr/local/lib/libxxx.so. – thoni56 Apr 12 '17 at 08:58
  • @thoni56 Cmake can retrieve if it's compiling 32 or 64 bit, see [here](http://stackoverflow.com/a/20639201/2436175). – Antonio Apr 12 '17 at 09:05
  • @Antonio, yes, but it doesn't do anything with that knowledge, as far as I can see. I have to test that in CMakeList.txt:s and modify install paths etc. according to what I know about them, right? And that leads the CMakeList.txt:s to know about conventions for platforms, which it shouldn't. Or am I missing something? – thoni56 Apr 12 '17 at 09:08
  • @Antonio, and more over, isn't CMAKE_SIZEOF_VOID the size of void on this platform? I need what the compilers are compiling for. – thoni56 Apr 12 '17 at 09:09
  • @thoni56 `CMAKE_SIZEOF_VOID` is retrieved testing the compiler, so it is indeed referring to the target platform. – Antonio Apr 12 '17 at 09:10
  • @thoni56 `I have to test that in CMakeList.txt:s and modify install paths etc. according to what I know about them, right?`That's the way I would handle the problem, I am not aware if there's any other way. – Antonio Apr 12 '17 at 09:12

1 Answers1

3

tl;dr; CMake does not have a general mechanism for forcing 32- or 64-bit compilation. You do that with selection of compilers or compilation switches.

But CMake can actually find out if the compilation is for 64- or 32-bit (or probably many other word lengths too) target. As described in this answer and the CMake docs you should use:

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    message (STATUS "Compiling for 64-bit")
endif()

That is probably the underlying mechanism for "when building 64-bit binaries". But there are no variables explicitly for this.

NOTE that the variable CMAKE_SIZEOF_VOID_P is cached so that if you alter compiler options, say CMAKE_C_FLAGS to use '-m32' for a 32-bit compile it won't affect CMAKE_SIZEOF_VOID_P unless you clean your build directory.

So, in a way there seems to be a somewhat general mechanism for 32/64-bit handling. It is used in some situations, but to use it more extensively you have to handle that in your CMakeLists, which is not great.

Community
  • 1
  • 1
thoni56
  • 3,145
  • 3
  • 31
  • 49
  • **This is wrong, at least on Windows. There is actually NO way for CMake to figure out what the target platform of the chosen generator is.** Many more notes can be found in the comment section of this other answer: https://stackoverflow.com/questions/39258250/how-to-detect-if-64-bit-msvc-with-cmake/39258787#39258787 – Mitch McMabers Dec 09 '19 at 08:33
  • Thanks for you comment @MitchMcMabers. Yes, there seems to be no guaranteed way for *CMake* to do that, as my answer also tried to say. I haven't tried the "hack" for a while so it might not work anymore. However, as the _CMAKE_SIZEOF_VOID_P_ is determined by a try compile, I would assume that you, as the CMakeList.txt-author could figure it out. But that is providing that your compiler as setup correctly at the time of the try compile, and a lot of other if's and but's. So sure, YMMV. Thanks for the link to the other Q&A. – thoni56 Dec 09 '19 at 08:40
  • Hmm, thanks for your reply. Maybe it's possible to make `CMAKE_SIZEOF_VOID_P` work by triggering a `try_run()` to start the compiler. But according to this http://cmake.3232098.n2.nabble.com/CMAKE-SIZEOF-VOID-P-question-td7595355.html that's unreliable too because the bitness is cached so even if you do a 32-bit compile, the `CMAKE_SIZEOF_VOID_P` can be cached as 64-bit. It's a mess. If someone ever figures out a good solution, I hope they share it with the world! ;-) – Mitch McMabers Dec 09 '19 at 09:51