0

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?

bumbur
  • 302
  • 3
  • 9
  • Remove all of the POSIX System calls and Replace them with Win32/64 System calls best way or you can use Boost or QT libraries that compile on both and handle the same operations – TheBetaProgrammer Dec 23 '14 at 20:21
  • I believe I have to use 'standard' libraries. After replacing those calls it will work on windows but not on linux anymore. I have to write source code which can be compiled in both systems and work in both systems. – bumbur Dec 23 '14 at 20:26
  • Personally I prefer using MinGW--a set of POSIX libraries for Widows. I've made programs and libraries with it. There's also Cygwin, but I think that's a bit too heavyweight. The other problem is that Microsoft no longer makes a C compiler. VS only compiles C++, or programs that use a small pre-1999 subset of C. – Lee Daniel Crocker Dec 23 '14 at 21:29
  • @LeeDanielCrocker: Not entirely true, VS 2013 introduced support for C99 compound literals, designated initializers, as well as library features (see [this](http://msdn.microsoft.com/en-us/library/hh409293.aspx) and [this](http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx)). Of course you can compile C code with it. I agree though, that it's far from being fully C99-compliant and C11 seems to be completely ignored by them (of course POSIX is ignored too). – Grzegorz Szpetkowski Dec 23 '14 at 21:47
  • I believe I downloaded VS after this announcement and it still failed to compile my library, but I'll look more closely. – Lee Daniel Crocker Dec 23 '14 at 21:50
  • simple/robust method. at beginning, have code that determines if current OS is Linux or Windoze. Use the result of that in a if/else construct to execute one or the other set of the two code blocks. If the same code on disk must work with both systems, try using the above. Otherwise, if there can be two executables, with the same name, one on each OS, use #if to generate separate code, depending on which OS is being used for the compile/link – user3629249 Dec 23 '14 at 22:48

3 Answers3

2

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.

Community
  • 1
  • 1
  • Yes, I am required to use VS. I think I have to use some #ifdef to detect which system is used. But then I'm not sure what to do. I could make two big if declarations, one would execute if system is linux and the other one if windows, but code like that won't compile. – bumbur Dec 23 '14 at 20:30
  • What about using dynamic libraries? I've never used them but is there any solution which involves using them? – bumbur Dec 23 '14 at 20:31
  • Does visual studio run under Linux??? *I* am not aware of such an implementation of VS. – user3629249 Dec 23 '14 at 22:50
1

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.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • You could still use pthreads implementations layered on Windows threads. If you wish to use bash et al without falling under cygwin license you have choices of cygwin mingw cross compiler or Windows 10 bash support. It's not clear that even Microsoft is trying to set insurmountable obstacles. – tim18 May 22 '16 at 00:10
1

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.

Philip
  • 5,795
  • 3
  • 33
  • 68