Ron*_*nin 47 c c++ linux windows
我正在编写一个跨平台程序.我希望这个程序在Windows和Linux下运行,所以我有两个不同的代码段用于这两个平台.如果操作系统是Windows,我想要运行第一个代码段; 如果它是Linux,那么我希望第二个代码段运行.
所以我编写了以下代码,但在Windows和Linux上构建时都会出错.我该怎么做才能解决它?
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
#define OS_Windows 0
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#elif defined(_WIN32) || defined(WIN32) /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
#define OS_Windows 1
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define DIV 1048576
#define WIDTH 7
#endif
int main(int argc, char *argv[])
{
if(OS_Windows)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
}
else if(!OS_Windows) // if OS is unix
{
char cmd[30];
int flag = 0;
FILE *fp;
char line[130];
int memTotal, memFree, memUsed;
flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
{
flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else
printf("not found\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
use*_*016 49
它通常是这样做的(或多或少):
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define DIV 1048576
#define WIDTH 7
#endif
#ifdef linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif
int main(int argc, char *argv[])
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
#endif
#ifdef linux
char cmd[30];
int flag = 0;
FILE *fp;
char line[130];
int TotalMem, TotalFree, TotalUsed;
flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
{
flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else
printf("not found\n");
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这样,只有linux的代码才能在linux平台上编译,只有windows代码才能在windows平台上编译.
Zau*_*bov 18
您应该在代码中使用相同#ifdef的if(OS_Windows)逻辑而不是逻辑:
#ifdef __unix__
...
#elif defined(_WIN32) || defined(WIN32)
#define OS_Windows
#endif
int main(int argc, char *argv[])
{
#ifdef OS_Windows
/* Windows code */
#else
/* GNU/Linux code */
#endif
}
Run Code Online (Sandbox Code Playgroud)
And*_*rew 12
我在这里看到了很多不同的解决方案,这让我感到不舒服......如果他们在Linux上运行但不在Windows或Windows上而不是在Linux上工作怎么办?如果他们只在某些编译器上工作怎么办?等等.
所以我找到了这个链接,我喜欢这个链接:http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
看起来这些是最好的(使用#ifdef,#endif等):
_WIN32 对于Windows 32位_WIN64 对于Windows 64位__unix__ 对于Unix您将变量(存在于运行时中)与预处理程序符号(仅在编译期间存在)混淆.
在你做了类似的事情后#define OS_Windows 1,你不能使用符号OS_Windows作为变量并把它放在if()s里......我的意思是,你可以,但它会在编译时扩展if (1).
对于跨平台项目,您必须使用#if或#ifdef确保编译器为给定的OS选择正确的代码部分并仅编译它.
就像是:
void openWindow() {
#if OS_Windows
// windows-specific code goes here
#else
// linux-specific code
#endif
}
Run Code Online (Sandbox Code Playgroud)