I have written a code which should work both in Linux and Windows. For windows I have written a specific program that uses functions and capabilities only available in windows i.e. it does not work in Linux.
My program header:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined (_WIN32)
# include <direct.h>
# include <windows.h>
# define getcwd _getcwd
# define PATH_SEPARATOR "\\"
# define snprintf _snprintf
# define flag 2 //flag is 2 in windows
# define strdup _strdup
# define fileno _fileno
#else
# include <unistd.h>
# include <dirent.h>
# define PATH_SEPARATOR "/"
# define flag 1//on linux the flag is 1
# define sprintf_s snprintf
# define getch() getchar()
# define malloc (char*)malloc
#endif
Now I have a function that only works with windows and gives loads of errors when I try to compile on Linux.
void funtionForWindowsOnly(){
//some things that I can only do on windows for Linux I will write a new program
}
So in my main I have done this
main(){
if(flag==2)//i.e. in case of windows do this
{
funtionForWindowsOnly();
}
}
But when I try to do this Linux gives me loads of errors as it is trying to compile the function
void funtionForWindowsOnly();
My question is how can I leave this function funtionForWindowsOnly() in my program and my program still successfully compiles using Linux?
I think there is a way of doing #ifdef or something like this using preprocessors. Or maybe there is another simple way. Please help. If you require any further information please let me know.