If you use NetBeans under linux or Mac OS, replace step one of this guide with the instructions on the library’s manual page.
I’m currently trying to get comfortable with NetBeans as an IDE for C, under windows. NetBeans 7.01 had some deal-breaker issues with C (failed to identify valid struct typedefs), so I had to upgrade to 7.1rc2 (and 7.1rc2 has some font issues… but I’m hoping they fix that by January). As an exercise I’m moving all the CS50 C sources I already wrote (in EditPlus and Scite) into NetBeans projects.
Before we begin, Install:
The highlights for linking with the CS50 library
- Make a new ‘C/C++ static library’ project and put the cs50.c/cs50.h files in it.
Build a ‘Debug’ version of the library. Find the .a file you just built under yourProjectFolder/dist/Debug/Cygwin-Windows/libcs50-library-c-3.1.a, rename it to libcs50.a and move it to C:\cygwin\lib\
- Create a new C/C++ project.
- Right click on the project and select properties.
- Under Build>C Compiler add -std=c99 in ‘Additional Options’
- Under Build>Linker add ‘-lcs50‘ in ‘Libraries‘ (to do that, use the ‘Add option‘ button inside the libraries dialog and select ‘Other Option‘).
DO NOT add ‘-lcs50‘ in the ‘Additional Options‘ field, although that looks more appropriate it is not the right place. (Why?: order of cli arguments in the gcc command matters. ie: gcc […] file.c -lcs50 works but: gcc -lcs50 […] file.c does not. Why does the NetBeans dialog have this counter-intuitive setup?: no idea)
And you’re done! You can now: use #include <cs50.h> in the source inside this project, Build, Run the project.
- Optionally: Make a new NetBeans Project Template for CS50 projects. It’s actually easier than the detailed tutorials make it look. But is a bit counter-intuitive if you’re used to simply adding a new .template file in other, simpler erm.. editors. In NetBeans the way to make a new project type involves making a new NetBeans modulethat contains all the information for that project type.
- Create a new C/C++ project as described above.
- Create a new NetBeans module project.
- In the Basic Module Configuration panel, use something like: org.netbeans.modules.cs50ProjectTemplate.
- After you create the module project, add the dependencies mentioned here. (You don’t need the Web API as that example does)
- Right click on the module project and select New > Other.
And add the customized C/C++ project you created as a project template.
You probably want to edit some more stuff, poke around. When you’re certain you’re done, right click on the module project and select: Install/Reload in Development IDE.
WARNING: Make sure to TEST the module –and the project template– first (rc>Run) before you Install it! (rc>Install/Reload). If you install a broken module the IDE may end up broken and you might need to manually clean it up (or uninstall/reinstall).
The same config setting as described for -lcs50 also works for -lcrypt or -l<anything>.
There may be some alternate approach to this, but this is how I managed it.
You should also copy the sources cs50.c and cs50.h into C:\cygwin\usr\include\ so the debugger (gddb) can look them up if/when needed.
As a general note: When working in NetBeans you have to make one project per executable. You can’t have one ‘pset1’ project with all the sources you are asked to make in pset1, instead you have to make distinct projects like: ‘pset1_skittles’, ‘pset1_chart’ … etc, for each and every ‘executable’ .c source with main() in it.
If you don’t use NetBeans you can still use Cygwin’s GCC to compile your .c sources from the Cygwin terminal, in which case you probably want to do the very first step above. Compile the library to libcs50.a and move it to C:\cygwin\lib\ do this from a Cygwin terminal:
gcc -c -ggdb -std=c99 cs50.c -o cs50.o ar rcs libcs50.a cs50.o
Steps to do this for MinGW instead of Cygwin are almost identical, just replace C:\cygwin\lib\ with C:\MinGW\lib\ and C:\cygwin\usr\include\ with C:\MinGW\include
A must-have for all Cygwin users is a shell extension for windows explorer that allows you to right-click a folder and select ‘Bash Prompt Here’. (re-)Run the Cygwin installer and install ‘chere’. Then in a Cygwin terminal enter: ‘chere -i -t mintty’. Now when you right click on folders and select ‘Bash Prompt Here’ you get a mintty terminal at the folder you right clicked. (mintty is my preference over the sluggish default cmd terminal.)
The easiest option is to forget about building in windows and use the CS50 Appliance instead. Which is what I did originally: I Wrote the source in Editplus and Scite and used a shared folder with the VM so I could just Alt-Tab to the VM and gcc/make the sources without much headache.