bumbur
bumbur

Reputation: 311

How to make program which can be compiled in gcc (Linux) and VS (Windows)?

I have to write program which changes extensions of specific files in a folder. I've already done Linux version (which compiles with gcc) and now I wonder how should I edit it to be able to compile it in Windows (in VS) and make it work there also. It has to be one program (I can't do 2 versions, one for gcc and one for VS).

Language I have to use is C.

What I use there:

pthread.h library (multi-threading)

dirent.h library (opendir, readdir, rewinddir)

unistd.h library (chdir, usleep)

So what should I do to make it compile and work in windows and linux with the same source code on both operating systems? I know I have to scan folder and change extension with different functions in windows, but how to combine all of this into one file?

Upvotes: 0

Views: 1062

Answers (3)

Philip
Philip

Reputation: 5917

If you have to supply the same source tree to both compilers, what about the following approach:

#ifdef linux
#include <scanner_linux.c>
#else
#include <scanner_win.c>
#endif

This way, you can have two different source files, yet you are able to present both compilers with the same input file.

When I have to do this sort of Linux/Windows compile, I usually use cygwin. It's easily installed, and Windows users without Cygwin just need the cygwin.dll.

Upvotes: 1

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37954

The general problem is that Windows is not POSIX-compliant. If you don't want to even touch your code (that is, rewriting POSIX calls with their counterparts on Win and adjusting your code with preprocessor's conditional directives), the you might try adding Cygwin's cygwin1.dll, so POSIX calls are remapped with it. However you may not be happy with Cygwin's license restrictions.

See https://www.cygwin.com/faq.html#faq.programming.msvs-mingw for more reference.

Upvotes: 1

Joshua David
Joshua David

Reputation: 60

This may be too broad a question for stack overflow. There are numerous possible ways to approach this problem. Are you required to use VS? One approach would be to build and install gcc on windows, as described here.

Another possibility is to separate out the bulk of the code into multiple c files, and simply #ifdef lots of things.

You may find some more general useful info in this answer.

Upvotes: 2

Related Questions