1

This is what my current project looks like and I am trying to build dlls from this project. The problem I am having is I want to build lib1 as 64 bit dll and lib2 as 32 bit dll. lib1 gets built with 64 bit dll without any problem, but I cannot seem to build 32 bit dll of lib2.

.
├── CMakeLists.txt
├── lib1
│   ├── CMakeLists.txt
│   └── file1.cpp
└── lib2
    ├── CMakeLists.txt
    └── file2.cpp

2 directories, 5 files

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(multi_arch_lib)

add_subdirectory(lib1)
add_subdirectory(lib2)

lib1/CMakeLists.txt

set(TARGET lib1)
set(SRC file1.cpp)


add_library(${TARGET} SHARED ${SRC})

set_target_properties(${TARGET} PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        )

lib2/CMakeLists.txt (I wan to build 32 bit dll out of this)

set(TARGET lib2)
set(SRC file2.cpp)

#set(CMAKE_GENERATOR_PLATFORM Win32 CACHE INTERNAL "")

add_library(${TARGET} SHARED ${SRC})

set_target_properties(${TARGET} PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/my_lib
        COMPILE_OPTIONS  "-m32"  LINK_OPTIONS "-m32"
        )
# unset back to 64 bit
#set(CMAKE_GENERATOR_PLATFORM x64 CACHE INTERNAL "")

I have tried CMAKE_GENERATOR_PLATFORM and COMPILE_OPTIONS "-m32" LINK_OPTIONS "-m32" but both seem to always create 64 bit.

$file lib2/my_lib/Release/lib2.dll
lib2/my_lib/Release/lib2.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows

file1.cpp

void function1(){}

file2.cpp

void function2(){}

To generate solution file I do cmake -A x64 -S . -B Build

boka
  • 69
  • 1
  • 10
  • Does this answer your question? [The proper way of forcing a 32-bit compile using CMake](https://stackoverflow.com/questions/5805874/the-proper-way-of-forcing-a-32-bit-compile-using-cmake). You need to set `COMPILE_OPTIONS` and `LINK_OPTIONS` target properties. Note that `COMPILE_FLAGS` and `LINK_FLAGS` properties in accepted answer are now **deprecated** properties. – Kevin Jul 24 '20 at 19:52
  • https://stackoverflow.com/questions/5805874/the-proper-way-of-forcing-a-32-bit-compile-using-cmake This link may help. – jnkarate Jul 24 '20 at 19:53
  • @squareskittles I tried COMPILE_OPTIONS and LINK_OPTIONS but I still get 64bit dll. – boka Jul 24 '20 at 19:58
  • 1
    Building 32 Vs 64 bit versions of code goes a lot deeper than just getting the compiler to spit out 32 or 64 bit versions of a binary. If your code internally relies on something being 32 or 64 bits wide, it'll likely break. If your code relies on stuff being aligned on certain bounds, it'll break. If your code assumes it can count variables to certain values, it may break. Etc etc. There's a *lot* more to it than just getting CMake to do what you want. – Jesper Juhl Jul 24 '20 at 20:02
  • @JesperJuhl The code that I am trying to compile are those simple void functions. – boka Jul 24 '20 at 20:05
  • What compiler are you using? Does your compiler have multilib support? – Stephen Newell Jul 24 '20 at 20:05
  • @StephenNewell `-- The C compiler identification is MSVC 19.16.27041.0 -- The CXX compiler identification is MSVC 19.16.27041.0` – boka Jul 24 '20 at 20:07
  • now sure about the multilib support. – boka Jul 24 '20 at 20:07
  • `-m32` seems like a gcc flag. Check what flags visual studio uses to compile in 32bit mode. – Stephen Newell Jul 24 '20 at 20:08
  • @StephenNewell so I added `/MACHINE:X86` instead of `-m32`, now it says ignoring x86, and continues with x64 build . I know when I run `cmake -A x64 -S . -B` to generate a solution file, I do mention x64. I have to do this because I want lib1 still to be 64 bit. – boka Jul 24 '20 at 21:26
  • Sounds like Visual Studio is overriding this setting. I don't have access to a Windows machine to test, but there might not be a clean solution for this. – Stephen Newell Jul 24 '20 at 21:30

0 Answers0