Thread: Compiling C with a Makefile in Visual Studio

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Unhappy Compiling C with a Makefile in Visual Studio

    I've been searching for hours and I still can't figure out how to do this so I finally just registered and asked.

    I'm doing a project for college. We were given a bunch of C files (to modify) and H files and a Makefile. We were told to execute the Makefile using "make" in order to compile the program.

    I'm pulling my hair out trying to figure out how to do this. The Makefile itself is very short and simple so I'll post it here:

    Code:
    all:
    	gcc -g test.c fs.c ext.c -o sfs
    witha1:
    	gcc -g a1/caching.c a1/mydisk.c fs.c test.c exta1.c -o sfs
    clean:
    	rm -rf *.out *.o a1/*.o sfs storage
    I've tried installing MinGW and GNUwin32 but they don't seem to be doing anything. Can somebody please tell me some basic instructions on how to do this? It would really make my day.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Are you supposed to be using "Visual Studio" as you put in the thread title?
    nmake goes with "Visual Studio".

    Decide what Compiler you are supposed to be using; this might require you to decide on the Operation System first.

    I suggest using MSys or Cygwin if you really are supposed to use make on Windows OS.

    Link to Cygwin
    Link to MSYS | MinGW

    Tim S.
    Last edited by stahta01; 10-19-2013 at 12:27 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The easiest way to do this is to run from the Visual Studio console window. For example, click on start / programs / Visual Studio 2010 / Visual Studio 2010 Tools / Visual Studio 2010 Command Prompt. This opens up a console window with the environment variables set for Visual Studio 2010. Then run nmake (nmake.exe is Microsoft's version of make). You can run nmake /? to display the options.

    Instead of gcc, use cl /Ox /c.
    You'll need to link the objects using link or cl with the .obj files.
    Instead of rm, use del.
    Last edited by rcgldr; 10-19-2013 at 12:31 PM.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Are you supposed to be using "Visual Studio" as you put in the thread title?
    nmake goes with "Visual Studio".

    Decide what Compiler you are supposed to be using; this might require you to decide on the Operation System first.

    I suggest using MSys or Cygwin if you really are supposed to use make on Windows OS.

    Link to Cygwin
    Link to MSYS | MinGW

    Tim S.
    We weren't told what software to use but we can download VS for free at my college and it seems like one of the most popular IDEs. I installed Msys with MinGW but I have no idea what it does or how to use it.

    Quote Originally Posted by rcgldr View Post
    The easiest way to do this is to run from the Visual Studio console window. For example, click on start / programs / Visual Studio 2010 / Visual Studio 2010 Tools / Visual Studio 2010 Command Prompt. This opens up a console window with the environment variables set for Visual Studio 2010. Then run nmake (nmake.exe is Microsoft's version of make). You can run nmake /? to display the options.

    Instead of gcc, use cl /Ox /c.
    You'll need to link the objects using link.
    Instead of rm, use del.
    I went into the Developer Command Prompt (I'm using the 2013 edition by the way) and tried to use nmake to run it. I get a message saying

    "gcc is not recognised as an internal or external command" though. We have to re-submit all our files and we're not allowed to edit the Makefile. Is there any way to run gcc and so on in Windows?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cian View Post
    We weren't told what software to use but we can download VS for free at my college and it seems like one of the most popular IDEs. I installed Msys with MinGW but I have no idea what it does or how to use it.



    I went into the Developer Command Prompt (I'm using the 2013 edition by the way) and tried to use nmake to run it. I get a message saying

    "gcc is not recognised as an internal or external command" though. We have to re-submit all our files and we're not allowed to edit the Makefile. Is there any way to run gcc and so on in Windows?
    Yes, MSys and Cygwin are two of the many ways.

    You use the command prompt/batch file to open the command prompt with each; in neither MSys or Cygwin is the normal cmd.exe prompt used directly in most cases.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Yes, MSys and Cygwin are two of the many ways.

    You use the command prompt/batch file to open the command prompt with each; in neither MSys or Cygwin is the normal cmd.exe prompt used directly in most cases.

    Tim S.
    I found the batch file for Msys but I don't know how to use it and couldn't find any guides online. Also, how can I use it from an IDE like Visual Studio?

    EDIT: Okay I got nmake to work in the command prompt. It turned out that I needed to add the location of the MinGW folder to the Environmental Variables. I do still have the problem in Visual Studio though of getting it to compile the Makefile. I set up the Makefile Project and enter "nmake" as the build command line but it says it can't find the makefile. How can I point it in the right direction or where could I copy the makefile to so that it would follow the instructions?
    Last edited by cian; 10-19-2013 at 01:28 PM.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You will NEVER get Visual Studio to work using that Makefile without doing stuff that violates the class rules!
    This is because you said the rules was to not modify the makefile.


    NOTE: If you are allowed to modify the makefile to use another compiler instead of gcc then you could use the compiler from Visual Studio.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    NOTE: Every class ( at my small College) that had a makefile like that had to remotely connect to a Unix or Linux computer and do the project that way.
    Are you sure that is NOT true for your class?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Found an old MSys installation on my PC, to start MSys double click on the file "msys.bat"

    Note: I suggest copying the project files in a project folder under the MSys home/USERNAME folder.
    USERNAME is your username on the Computer!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    NOTE: Every class ( at my small College) that had a makefile like that had to remotely connect to a Unix or Linux computer and do the project that way.
    Are you sure that is NOT true for your class?

    Tim S.

    They told us to either use Visual Studio or Vim+GDB if we were using Windows. Nothing about connecting to Unix or Linux remotely. I did get somewhere with VS though. I set up MinGW properly and managed to get Visual Studio to compile by setting up a new Makefile project. I was then able to tell it to run the command "nmake" which compiled the program, although I had to put all the files into the same folder as the project. The only problem now is in running it.

    Visual Studio likes to create an exe file with the name of the project and then run it for debugging. Since the compiled binary file is make by the makefile (and called "sfs"), it leaves Visual Studio searching for an exe file with the same name as the project. This means that I can't debug line-by-line. Is there any way to point Visual Studio to the binary created by the makefile?

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The problem is not only that it can't find the binary.

    GCC generates debugging symbols in a different format than VS, so you would need to use GDB to debug code generated by GCC. I'm fairly certain you won't be able to make VS use GDB.

    Just use one or the other system. Why try to hack VS to use GNU tools? Why not just use any text editor (like Notepad++) and compile on command line, if you want or have to use GNU tools?

    If you want GUI debugging there are many GDB GUI frontends, too.

    You can also use an IDE that is designed to work with GNU tools, like Eclipse or QtCreator or Code::Blocks.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would say - write, compile and debug using VS GUI (no makefile at all)
    And when all is done and well - compile it and check that no warning are shown by gcc using provided makefile
    Fix warnings and check that generated exe work the same as VS generated one...

    At home I created small svn server installation where I can commit update from 2 locations - one fro VS compile on Windows another for GCC compilation of the same project on Ubuntu...
    Ubuntu you can install inside VirtualBox to avoid usage of different computer

    If you'll go with minGW option of gcc on Windows - you can just copy your source files from VS folder into minGW project folder.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by cian View Post
    tried to use nmake to run it. I get a message saying "gcc is not recognised as an internal or external command" though.
    You could make batch files. gcc.bat would run cl.exe and rm.bat would do deletes. The gcc.bat file would need to ignore the -g flag (don't use the first batch file parameter which is %1).
    Last edited by rcgldr; 10-20-2013 at 02:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Info on MakeFile & Depencies Visual Studio
    By MaSSaSLaYeR in forum C Programming
    Replies: 2
    Last Post: 12-04-2011, 02:42 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Beginner question about compiling and visual studio
    By GibsMe in forum C Programming
    Replies: 4
    Last Post: 09-13-2009, 03:36 AM
  4. Compiling Error in Visual Studio 2005
    By JeroG in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2009, 09:26 AM
  5. Compiling from DOS Prompt using Visual Studio Express 2005
    By The SharK in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 01:24 AM