PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

[FIXED] How to build a 32 bit linux module remotely from visual studio?

 May 14, 2022     c++, cmake, cross-platform, ubuntu, visual-studio     No comments   

Issue

I have been trying to create a cross-platform project. Both 32 bit and 64 bit binaries (dlls) need to be built. I am using Visual Studio and selecting x86-Release or x64-Release solves the problem for windows.

But for linux it is always building 64 bit binaries.

After searching, i found that g++ multilib needs to be installed and -m32 flag needs to be passed to both the compiler and linker. I found that

set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

will solve the problem.

So to build a 32 bit linux binary (MODULE with .so ending), i added the above code.

I also have this code beneath the above code

if(WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        message("WIN32 x64")
    else()
        message("WIN32 x32")
    endif()
else(WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        message("LINUX x64")
    else()
        message("LINUX x32")
    endif()
endif(WIN32)

However, it outputs

1> [CMake] LINUX x64

So it is still building 32 bit build.

I have both g++ and g++ multilib installed, but not sure visual studio is calling which one. could that be the problem?


Solution

As mentioned in comments, it was found that CMAKE_SIZEOF_VOID_P is not able to detect if m32 flag is passed. So i modified the code like this

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(OUTPUT_BITNESS 64)
else()
    set(OUTPUT_BITNESS 32)
endif()
if(FORCE_32)
    set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
    set(OUTPUT_BITNESS 32)
endif()

if(WIN32)
    if(OUTPUT_BITNESS EQUAL 64)
        message("WIN32 x64")
    else()
        message("WIN32 x32")
    endif()
else(WIN32)
    if(OUTPUT_BITNESS EQUAL 64)
        message("LINUX x64")
    else()
        message("LINUX x32")
    endif()
endif(WIN32

So to compile for 32 bit, FORCE_32 variable must be set to 1.



Answered By - habi
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing