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 7743297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:29:52+00:00 2026-06-01T09:29:52+00:00

in header file I have defined the following function #ifndef OS_H #define OS_H #include

  • 0

in header file I have defined the following function

#ifndef OS_H
#define OS_H

#include <string>
#include <vector>

int GetDirectoryFiles(std::string directory, std::vector<std::string> &files);

#endif /* OS_H */

I would like have separate implementations for each platform

#include "os.h"
#include <iostream>

#ifdef OSWIN
#include <windows.h>
#elif OSLINUX
#include <dirent.h>
#endif

int GetDirectoryFiles(std::string directory, std::vector<std::string> &files)
#ifdef OSLINUX
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}
#elif OSWIN
{
    std::clog << "entered!" << std::endl;

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = FindFirstFile(directory.c_str(), &FindFileData);
    if ( hFind == INVALID_HANDLE_VALUE )
        return -1;

    do {
        files.push_back(FindFileData.cFileName);
    } while(FindNextFile(hFind, &FindFileData) != 0);
    FindClose( hFind );

    return 0;
}
#endif

Is there more elegant way to do this (what is considered good style).

  • 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-06-01T09:29:53+00:00Added an answer on June 1, 2026 at 9:29 am

    The typical way to handle this would be to have the function declaration in a shared header file, then have one cpp file per platform that contains the different platform-specific definitions of this function. Your build system then only makes and links in the corresponding platform-specific cpp file.

    ie

    Filesystem.h

    #include <string>
    #include <vector>
    
    int GetDirectoryFiles(std::string directory, std::vector<std::string> &files);
    

    Filesystem_Windows.cpp:

    #include "Filesystem.h"
    
    int GetDirectoryFiles(std::string directory, std::vector<std::string> &files);
    {
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind = FindFirstFile(directory.c_str(), &FindFileData);
        ...    
        return 0;
    }
    

    Filesystem_Linux.cpp:

    #include "Filesystem.h"
    
    int GetDirectoryFiles(std::string directory, std::vector<std::string> &files);
    {
        DIR *dp;
        struct dirent *dirp;
        ...
        return 0;
    
    }
    

    See the source tree of Qt, which is a very popular cross-platform C++ library, as an example of this kind of structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
I have a header file with all the enums listed (#ifndef #define #endif construct
Suppose I have the following defined in a header file: namespace MyNamespace { Class
I have a header file... #include <SFML\Graphics.hpp> #include <SFML\Graphics\Drawable.hpp> #include <SFML\System.hpp> #include <iostream> #ifndef
Suppose I have a list of #define s in a header file for an
I have C++ project (VS2005) which includes header file with version number in #define
I have C header file containing the following type definition: // example.h typedef struct
I have the following chunk of a header file BKE_mesh.h: /* Connectivity data */
I'm trying to read the bitmap header. I have defined the following struct: typedef

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.