Praktische Implementierung
graph TD
A[Designphase] --> B[Plattformdetektion]
A --> C[Schnittstellendefinition]
B --> D[Bedingte Kompilierung]
C --> E[Implementierungsstrategie]
#ifndef CROSS_PLATFORM_FS_H
#define CROSS_PLATFORM_FS_H
#include <string>
#include <vector>
class CrossPlatformFileSystem {
public:
// Plattformunabhängige Schnittstelle
static bool createDirectory(const std::string& path);
static bool removeDirectory(const std::string& path);
static std::vector<std::string> listFiles(const std::string& directory);
private:
// Plattform-spezifische Implementierungsdetails
#ifdef __linux__
static bool linuxCreateDirectory(const std::string& path);
#elif defined(_WIN32)
static bool windowsCreateDirectory(const std::string& path);
#endif
};
#endif // CROSS_PLATFORM_FS_H
#include "CrossPlatformFS.h"
#ifdef __linux__
#include <sys/stat.h>
#include <dirent.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
bool CrossPlatformFileSystem::createDirectory(const std::string& path) {
#ifdef __linux__
return linuxCreateDirectory(path);
#elif defined(_WIN32)
return windowsCreateDirectory(path);
#else
#error Nicht unterstützte Plattform
#endif
}
#ifdef __linux__
bool CrossPlatformFileSystem::linuxCreateDirectory(const std::string& path) {
return mkdir(path.c_str(), 0755) == 0;
}
#endif
#ifdef _WIN32
bool CrossPlatformFileSystem::windowsCreateDirectory(const std::string& path) {
return CreateDirectoryA(path.c_str(), NULL) != 0;
}
#endif
Kompilierungsstrategien
Plattform |
Kompilierungsbefehl |
Schlüsselflags |
Linux |
g++ -std=c++17 -O2 |
-pthread |
Windows |
cl /std:c++17 /O2 |
/EHsc |
macOS |
clang++ -std=c++17 |
-stdlib=libc++ |
Fehlerbehandlung und Protokollierung
class PlatformLogger {
public:
static void log(const std::string& message) {
#ifdef __linux__
// Linux-spezifische Protokollierung
syslog(LOG_INFO, "%s", message.c_str());
#elif defined(_WIN32)
// Windows-spezifische Protokollierung
OutputDebugStringA(message.c_str());
#endif
}
};
Empfohlene Techniken
graph LR
A[Plattformübergreifende Entwicklung] --> B[Minimale plattform-spezifische Codebasis]
A --> C[Standard-C++-Funktionen]
A --> D[Abstraktionsschichten]
A --> E[Umfassende Tests]
LabEx Empfehlungen
Bei LabEx legen wir Wert auf:
- Die Verwendung von Standard-C++-Bibliotheken
- Die Implementierung portabler Abstraktionen
- Rigorose plattformübergreifende Tests
- Die Minimierung plattform-spezifischen Codes
Kompilierung und Tests
Beispiel-Kompilierungsskript
#!/bin/bash
## Plattformübergreifendes Kompilierungsskript
## Linux-Kompilierung
g++ -std=c++17 -O2 main.cpp CrossPlatformFS.cpp -o app_linux
## Windows-Kreuzkompilierung (mit mingw)
x86_64-w64-mingw32-g++ -std=c++17 -O2 main.cpp CrossPlatformFS.cpp -o app_windows.exe