Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:56+00:00 2026-05-26T11:09:56+00:00

In the multi-platform/os project I’m working on, I’m striving to simplify the platform specific

  • 0

In the multi-platform/os project I’m working on, I’m striving to simplify the platform specific code into subdirectories, with a common directory holding a common implementation. I have a prototype implementation in autotools, but want to move to a cmake implementation if possible. My current sticking point is how to support multiple OSes. My current filesystem structure looks like the following:

/* A common header file for all platforms that presents an interface */
include/my_socket_utils.h
include/my_time_utils.h

/* Platform specific source files */
src/common/my_socket_utils.cpp
src/linux/my_socket_utils.cpp 
src/vxworks/my_socket_utils.cpp 
src/qnx/my_socket_utils.cpp 

src/common/my_time_utils.cpp
src/vxworks/my_time_utils.cpp

The idea is that there is a common interface and a “common” implementation. The implementation is either a stub or a common implementation written to a posix standard that allows it to work for most platforms. Those platforms that require a custom implementation MAY override the common one, but it’s optional.

With autotools, I am able to achieve this using VPATH to set a source tree hierarchy, so I set:

VPATH=@srcdir@/src/@target_platform@;@srcdir@/src/common

This makes autotools look for the source file in src/@target_platform@ first, then, if it wasn’t found, grab it from src/common.

What is the cmake way to do this?

Update:
To help all those lost souls in need, this is what I ended up doing for the time being. I’m not sure it’s the best solution, but it works well enough.

FILE(GLOB common_files “src/common/.c“)
FILE(GLOB platform_files “src/${os}/.c)

Then, do the dirty n^2 algorithm to override. Not sure how to do any better in cmake “script”, but the number of files is low, so it’s plenty fast. The diag messages are, of course, optional.

#
# For each common file, check to see if a platform file exists to override it.
#
foreach(fqfn ${common_files})
    set(platform_override FALSE)
    get_filename_component(filename ${fqfn} NAME)
    # 
    # If filename exists in platform, override it with the platform,
    # otherwise fall back to the common implementation.  Oh for a real
    # language.
    #

    foreach(platform_fqfn ${platform_files})
        get_filename_component(platform_filename ${platform_fqfn} NAME)
        message("pf=${platform_filename} cf=${filename}")
        if(filename STREQUAL platform_filename)
            message("filename == platform_filename")
            list(APPEND proj_files ${platform_fqfn})
            set(platform_override TRUE)
        endif(filename STREQUAL platform_filename)
    endforeach(platform_fqfn ${platform_files})

    if(NOT ${platform_override})
        list(APPEND proj_files ${fqfn})
        message("Appended ${fqfn}")
    endif(NOT ${platform_override})
endforeach(fqfn ${common_files})

message("proj_files=${proj_files}")
add_executable (cc_dfi_main ${proj_files})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:09:57+00:00Added an answer on May 26, 2026 at 11:09 am

    One possible way is to define variable TARGET_BUILD_PLATFORM and set it to exact platform you want to build (linux/qnx/vxworks).

    set(PROJECT_NAME some_name_for_project)
    project(${PROJECT_NAME} CXX)
    
    file(GLOB COMMON_SRC ${PROJECT_SOURCE_DIR}/common/*.cpp)
    file(GLOB PLATFORM_SRC ${PROJECT_SOURCE_DIR}/${TARGET_BUILD_PLATFORM}/*.cpp)
    
    set(SRC_FILES ${COMMON_SRC} ${PLATFORM_SRC})
    
    add_executable(${PROJECT_NAME} ${SRC_FILES})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a few multi platform projects that all depend on common framework.
Many multi-platform applications seem to have common source code. How do builds generate platform
Today, in my C++ multi-platform code, I have a try-catch around every function. In
I'm working on a paper about multi-platform programming and I'd like to include sections
I'm working on a multithreaded multi-platform Qt-based program that uses libssh2. The program was
I'm working on a free, simple, hopefully multi-platform hotkey launcher tool, written in Ruby.
I'm working on a multi-platform occassionally connected app. Each instance of the app stores
I'm searching for an multi platform OpenGL framework that abstracts the creation of windows
I believe strongly in using unit-tests as part of building large multi-platform applications. We
I'm currently looking at which build server we should run up for a multi-platform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.