Compiling Audacity for Microsoft Windows (TM) using Microsoft Visual C++ (TM)

(For instructions on compiling Audacity under the Cygwin UNIX Emulation Layer,
scroll down to the bottom.)

Authors:
  Asger Ottar Alstrup
  Vince Busam
  Dave Fancella
  Vaughan Johnson
  Steve Jolly
  Markus Meyer
  Shane Mueller

================================================================
This version is currently accurate for Audacity version 1.2.3.
Last Updated: December 9, 2004
If the advice here is inaccurate or incomplete, please
email audacity-devel@lists.sourceforge.net.
================================================================

Audacity releases to date have been built with Visual C++ 6.0, 
with Service Pack 5.  If you require it, this patch is available from
Microsoft at

  http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp5/default.asp

Audacity can be built with Visual C++ .NET 7.1 (or 2003). Just 
build wxWidgets with the .NET C++, and then use 
"audacity.vcproj" instead of "audacity.dsw".

It is worth noting that Audacity uses VC++'s multithreaded
statically-linked run-time libraries.

A note regarding the library wxWidgets, which is used by Audacity:

  This library was formerly known as wxWindows, but its name has
  now changed to wxWidgets. At the time of writing, the current
  version 2.4.2 is advertised as "wxWidgets 2.4.2", but the file
  archives still contain the old name. It is expected that this will
  change in future releases, so some of the instructions below
  may or may not apply.

---------------------------
MSVC++ STEP 1:  Download wxWidgets
---------------------------
To simplify the implementation of a near-identical user
interface across platforms, Audacity uses classes and
resources from wxWidgets, a portable GUI framework for C++
(and Python).  You must download and build wxWidgets before
attempting to compile Audacity, or nothing will work!

wxWidgets is available from http://www.wxwidgets.org.
Similar to the Linux kernel, wxWidgets is available as
stable and development branches.  

Audacity uses wxWidgets 2.4.2 or greater for Microsoft
Windows (MSW),  available at "http://www.wxwidgets.org/".


------------------------
MSVC++ STEP 2:  Build wxWidgets
------------------------
When you have downloaded and installed wxWidgets, you must
build it.  First though, to be compatible with Audacity, you
must alter the build settings to use the multithreaded
statically-linked run-time libraries instead of the
multithreaded DLL run-time libraries.  This isn't hard:

1. Launch Visual C++ and open the workspace file called
   "wxWidgets.dsw" in the "src" directory of your wxWidgets
   installation.

2. Select "Project"->"Settings" (Alt+F7), choose "Win32 Release"
   in the "Settings For" combo box and select everything visible
   in the tree window on the left of the dialog box.

3. Now select the "C/C++" tab and change "Use run-time library"
   under the 'Code Generation' combo box to "Multithreaded".

4. Now select "Win32 Debug" from the "Settings For" combo and
   change the run-time library to "Debug Multithreaded".

5. Now you can click "OK" and build wxWidgets by selecting
   "Build"->"Batch Build" and clicking "Rebuild All".

This will build both Debug and Release versions of the
wxWidgets library.

--------------------------
MSVC++ STEP 3:  Download Audacity
--------------------------
Now you can compile Audacity.  If you want to checkout a
copy of the latest source code from CVS (assuming you
haven't already!), there are instructions at
http://audacity.sourceforge.net/cvs.php. Run Visual C++ and
load the Audacity workspace: it's "audacity.dsw" in the
"win" directory of the Audacity source tree.

--------------------------------------------
MSVC++ STEP 4:  Set wxWidgets location for Audacity
--------------------------------------------
Before you can start the build process, you must update the
project settings to tell Visual C++ where to find wxWidgets.
There are three settings that need changing:
    1.  Include Directory (e.g., "C:\wxWidgets-2.4.2\include")
    2.  Resource Directory (e.g., "C:\wxWidgets-2.4.2\include")
    3.  Library Directory (e.g., "C:\wxWidgets-2.4.2\lib")

Fortunately, this is parameterized, using a Windows system 
environment variable. There are various ways to set these, so 
look in the Windows help system. You want to create an 
environment variable WXWIN, set to the top-level directory 
where you installed wxWidgets. For example, 
in your "autoexec.bat" file, you might have a line:

   set WXWIN=C:\wxWidgets-2.4.2

Note that you don't want a trailing backslash on that. In the 
Audacity project file, there are declarations like 

   $(WXWIN)\include

that get interpreted as, e.g., "C:\wxWidgets-2.4.2\include".

Once you have set the WXWIN environment variable correctly, 
the Audacity project file's declarations will do the rest.

-----------------------
MSVC++ STEP 5:  Build Audacity
-----------------------
Open the file "configwin.h" and change it according to the
third-party libraries you have installed. Audacity comes with
all libs necessary to build the default configuration, but
some functionality like OGG/Vorbis support can be activated
optionally and must be downloaded separately.

Select the configuration that you wish to build
("Audacity - Win32 Release" for general use, "Audacity -
Win32 Debug" for a slower but debuggable executable) from
the "Build"->"Set Active Configuration" dialog and choose
"Build"->"Rebuild All" to compile Audacity!

That's all - if this procedure doesn't work, you can ask for
help on the Audacity development mailing list:
audacity-devel@lists.sourceforge.net.  The first suggestion
is likely to be "try it again with a clean installation of
the wxWidgets source and the latest Audacity source from
CVS" though, so if that's not what you just attempted, you
might want to try that first... 

---------------------------
HELP! I get linker errors!
---------------------------
It happens that Audacity does compile, but during the linking
process you get a bunch of error messages like this:

  MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _free
  already defined in LIBCMTD.lib(dbgheap.obj)

This is usually caused by conflicting runtime library
settings. All libraries linked with Audacity must use the
multithreaded statically-linked libraries, as explained in
step 2 above. Make sure you set the correct library for
both Debug and Release builds. Also make sure you check all
other libraries too, like the Ogg Vorbis libraries, if you
have them installed.

You may want to use the /VERBOSE:LIB linker parameter to
get information about which library is causing the conflict.

If all else fails, use the following procedure to force
Audacity to link to the correct library:

Go to the "Project"->"Settings" menu item in Visual Studio.

Ignore lib. Select "Win32 Debug" or "Win32 Release" as
appropriate for the build you were doing from the "Settings For"
combo. Select only "Audacity" in the tree, the Link tab, and
"Input" in the combo box. Enter "libcmt.lib" in the
"Ignore libraries" box if you were attempting to perform a
Release build, or "libcmtd.lib" for a Debug build.

On some systems, you will need to ignore "msvcrt.lib" 
or "msvcrtd.lib" instead of "libcmtd.lib" or "libcmtd.lib",
depending on the library which actually causes the conflict.

Note that if you have configured all libraries correctly,
none of the above should actually be necessary.

-----------------------
MSVC++ (OPTIONAL) STEP 6:  Add VST support
-----------------------
Audacity does not support VST plug-ins by default, but 
here is how to enable it.

1. Download "VST Enabler.zip" from the Audacity website. 
    Decompress it. 

2. Put the "VST Enabler.dll" file in the Audacity Plug-ins folder.

3. Run Audacity and the plug-ins should appear in the 
    Generate, Effect, &/or Analyze menus.

================================================================
-------------------------------
Compiling Audacity under Cygwin
-------------------------------

Compiling Audacity under Cygwin is considered "experimental" at this time, and you
may notice a few problems when you do so.  Please be patient, and make sure to submit
bug reports to the development team.  If you follow these instructions closely, you
*should* be able to build Audacity.

These instructions will attempt to help you to compile using the
experimental additions that appeared sometime around the 1.2 release.  They will also
attempt to help you to compile Audacity yourself without the aforementioned support.
Make sure to read the instructions in their entirety before beginning, because no matter
which source tree you wind up using, all of these instructions are likely to assist you.

If you have problems building, make sure you've read all of these instructions.  At the
bottom there is a list of known bugs.  If the problem you encounter is on this list, then
there is also a workaround.  Just remember to be patient, and that I have successfully built
Audacity on 3 different Windows installations with 2 different versions of Windows (XP and
2000 Pro).  Knowing that it can be done is most of the battle.

---------------------------------
Cygwin STEP 1:  Installing Cygwin
---------------------------------

1.  If you have not already installed Cygwin, download it from http://www.cygwin.com .  If
you have plenty of bandwidth available, you should use the net installer.  Otherwise,
you might be better off ordering a CD.

2.  If you already have Cygwin installed, you should fire up the installer again and
compare your installed packages to the list of packages needed.

3.  Install the following packages: (This is probably not a complete list.  If you find that
                                     you have to install more than this, make sure to let
									 us know which ones, so they can be added to this list)
    Autoconf
	Autoconf-devel (Autoconf 2.53 is required if you need to rebuild 'configure')
	gettext-devel (for building the translation files.  If you don't want to build these,
	               you don't need this package)
	Automake
	Automake-devel
	binutils
	gcc (only tested version is 3.3.1)
	gcc-mingw
	make
	mingw-runtime
	zip (required by configure, although gzip is actually used)
	w32api

---------------------------------------
Cygwin STEP 2:  Download and install wxWidgets
---------------------------------------

1.  Go to http://www.wxwidgets.org/ and download wxWidgets v2.4.1 or greater.  Make
sure you download the "wxAll" package that contains source code for all ports.  The
Win32 port will not work for compiling audacity, since it was compiled and installed
to work with MS Visual C++.

2.  Untar the archive from the Cygwin command line, using a command like:
tar -xzvf (wxwindows).

3.  From the top-level of the wxWidgets source tree, type './configure'.
** For some reason, I haven't been able to get Audacity to link to wxWidgets statically
under Cygwin.  Since there are bigger fish to fry right now, I haven't dealt with it.  I
would appreciate if someone gets it to work if you let me know what you did.  To try this,
use './configure --disable-shared' for wxWidgets configuration.

When complete, type "make".  When that's complete, "make install".  If all goes well,
you will have wxWidgets installed in your Cygwin installation!

---------------------------------
Cygwin STEP 3:  Download Audacity
---------------------------------

1.  Go to http://audacity.sourceforge.net/ and download the Audacity source distribution.
Cygwin is currently experimental in the 1.2 branch and cvs head, but you must have at least
Audacity v1.2.0 sources.

2.  From a cygwin prompt, go to the tarball that you downloaded and use a command like
'tar -xzvf (audacity tarball)' to open it.

-----------------------------------------
Cygwin STEP 4:  Enable Ogg Vorbis Support
-----------------------------------------

Under Cygwin, Audacity does not compile out-of-the-box with Ogg Vorbis support.  You have
a couple of hoops to jump through if you want this.  Note, if you don't do this, you will
not have any audio codec available to Audacity!  Lame is untested at this time and is
assumed not to work.  If you try it with Lame, tell us!

By default, libogg and libvorbis configure with /usr/local as the prefix.  For some
weird and inexplicable reason (i.e. I haven't figured it out yet) this doesn't work!
GCC will not find the libogg headers when you go to compile libvorbis if you install it
in the default location.  On that token, GCC will not find libvorbis headers either, so
make sure you follow these instructions explicitly.

1.  First you have to compile libogg.  From the audacity source tree root, type
'cd lib-src/libogg'.

2.  ./configure --prefix=/usr

3.  make

4.  make install

5.  cd ../libvorbis  # yep, now we have to compile vorbis

6.  ./configure --prefix=/usr

7.  make

8.  make install

If all goes well, you will have Ogg Vorbis support in your Audacity executable.

------------------------------
Cygwin STEP 5:  Build Audacity
------------------------------

This is the tricky part.  :)

Audacity comes bundled with a number of libraries that it depends on.  Usually you will
compile with those rather than any system installed libraries.  If you would prefer to use
a system installed library, you'll have to pass --with-library=/path/to/library to configure.
There are two libraries that currently do not compile in cygwin.  Those are Nyquist and
libid3tag.  If you manage to build them, please send a patch (or instructions on how you
did it)!

If you didn't enable Ogg Vorbis support as described in the previous step, you will have to
pass --without-vorbis to configure, as shown.

1.  ./configure --without-nyquist --without-id3tag [--without-vorbis]
2.  make
3.  ./audacity
4.  If you have any problems or errors, read the next section.

--------------------------------------
Notes on building Audacity with Cygwin
--------------------------------------

These notes are provided because Cygwin support is experimental, and if you run into problems,
it might help to know what was needed to make it work on my computer.

The problems that appear when compiling Audacity under Cygwin are pretty consistent in how
they appear.  The win32 port is written under the assumption that MSVC++ will be the
compiler used, and is the current standard and supported method of building Audacity.
Therefore, the problems that appear are mostly related to various symbols defined to make
Audacity compile out of the box on MSVC++.  Other problems that surface will likely
be based on the fact that Cygwin passes itself to configure as a UNIX variant, and there
are various other symbols defined for UNIX variants.  GNU/Linux is the standard UNIX variant
supported by Audacity, so you will have problems similar to what someone using OpenBSD might
encounter.  The exception is Mac OS X, which is well-supported already.

Audacity uses a series of libraries that are either required or optional, and can be set
at compile time.  The ones that are optional have --without switches for configure, the others
do not.  So if a bundled library doesn't compile, your best bet is to try disabling the library
in your configure command line.  If that doesn't work, you'll have to investigate the library
to see why it's not compiling.

PortAudio is required, and a Makefile is provided with Audacity to build it.  PortMixer is not
required, but is recommended.  This Makefile is also provided with Audacity.  Note: to my
knowledge, the Makefile for PortAudio provided with Audacity is not present in the PortAudio
distribution.  The one used with Audacity compiles PortAudio as a static library, and the
current Cygwin Makefile in the PortAudio distribution compiles as a dynamic library.  Audacity
will compile with either one, but if you use the one provided with PortAudio you will have to
make sure the PortAudio dll is in Audacity's path.  Of course, you shouldn't have any problems,
so this is probably more information than you need.  :)

Libsamplerate is not supported at all with Cygwin, and it is doubtful that it will ever be
supported.  If make tries to build it, --disable it.  Use Libresample instead.

Libresample may require tweaking some #define statements in a fashion that is noted below.

Expat should compile out of the box.  If not, you might be better off trying to install the version
of expat that ships with Cygwin.  If configure fails to detect a system installed expat, this
is probably a bug and should be submitted to the Audacity developers.  You can use the system-installed
expat by passing --with-expat=system to ./configure.

Any other libraries are probably not required, and if they fail to compile you should be able
to --disable them with configure.

-----------
Configwin.h
-----------

Configwin.h is a file that exists in the win directory from the root of Audacity's source tree.
You have three options for getting gcc to find this file.  You can modify Audacity's Makefile to
include -I../win (or whatever a good path is that will point at the directory).  You can copy
or move the configwin.h file to Audacity's source directory.  Preferably, you will modify
configure.in to generate configwin.h in exactly the same fashion and location that it already
generates configunix.h.

A fourth option is to not use Configwin.h at all, but to modify the appropriate header files to
use configunix.h.  Since this might be similar to opening a can of worms, I don't recommend this
approach.

---------------------
Dealing with #defines
---------------------

While working with this build system, there were a few files that failed to compile.  Whenever
make bombs on a file, you need to carefully note the line it made the error on and open the
source file in a good syntax highlighting editor, or you can use Wordpad, which is neither good
nor syntax highlighting.  Find the appropriate line on which the error occurred.  Then scroll
up.  Chances are very good that the line that triggered the error is wrapped in a #ifndef,
#if defined, or #ifdef block.  The #define symbol is usually __WIN32__.  You will be able to
get it to compile by making the line read something like this:

#if defined(__WIN32__) && !defined(__CYGWIN__)

In order for that to work, you will need to make sure that configure sets the __CYGWIN__ symbol to
be defined.  You may be able to just add it to Audacity's Makefile, however.  If you chose to use
the existing Configwin.h file and not have it generated by configure, then you should be able
to add a #define in it that defines __CYGWIN__.

Ultimately, if you find any of these options too difficult or time-consuming, you might consider
just deleting the entire block of code that is afflicted.  Since that route will likely wind up
making the job hundreds of times more difficult than it actually is, it is not recommended.

-----------------
Linking Audacity
-----------------

The normal Unix flags for linking will not work with Audacity.  Well, they will but they won't
include everything that needs to be linked.  Before discussing what actually needs to be linked,
it would be beneficial to discuss the win32api and Cygwin.  This discussion is surrounded on top
and bottom by **********'s, so if you already know this information you can easily skip it.

************************************************************

Cygwin is capable of compiling Windows programs that do not depend on the Cygwin runtime.  It
does this by using the Mingw libraries and header files, which are available under the GPL.
However, in order to make these libraries available to GCC, which operates thinking that it's
running on a Unix system, the libraries must be stored and named in a location that GCC (or more
specifically, LD, the linker) will understand.

On GNU systems, and likely on proprietary Unix systems as well (although I don't know this as a
fact), a library that is to be linked dynamically will have the extension .so.  A library that is
to be linked statically has the extension .a.  Furthermore, in either case the text "lib" is
prepended to the library.  When you pass a -l switch to GCC, GCC transparently passes this switch
to ld, the linker.  The text associated with -l will name the library.  For example:  If you pass
-lfoobar, ld will receive it.  In order to actually find the library, ld must prepend "lib" and
append ".a", and then search in its known library locations.  So it will search for a file called
"libfoobar.a" in its known library locations.

The known library locations vary from system to system.  On some GNU/Linux systems, the known
library locations are stored in a special file called "ldconfig".  Cygwin does not, however.  It
appears to know by magic where the libraries are stored.  You will find them in /usr/lib,
/usr/local/lib, /usr/share/lib, and ~/.lib.  If you look around your Cygwin installation, you'll
find a directory /usr/lib/w32api.  In this directory is stored all of the Mingw libraries that
allow you to link to the win32api.  If you've ever worked with MSVC++, you'll probably recognize
the names of these libraries.  There's kernel32, odbc32, winmm, and the rest.  But they're named
with ld's peculiar naming convention, libkernel32.a, libodbc32.a, and libwinmm.a respectively in
this example.

*************************************************************

From the MSVC++ project file, here are the libraries that need to be linked statically for
Audacity to successfully build.

kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib wsock32.lib winmm.lib

You can also obtain this list by modifying the Audacity Makefile to use
`wx-config --static --libs` instead of `wx-config --libs` which it already uses.  Currently,
Audacity will not build with `wx-config --static --libs`.  If you get it to work, let us know!

----------------------
Miscellaneous Problems
----------------------

When I tried to get Audacity to compile with wxWidgets 2.4.0, I ran into trouble with large file
support.  This trouble is characterized by a linker error that looks like this:

obj/import/ImportMP3.o(.text+0x63b): In function
`_Z8input_cbPvP10mad_stream':
/home/dfancella/audacity/src/import/ImportMP3.cpp:278: undefined
reference to `wxFile::Read(void*, long long)'

This problem went away when compiled with wxWidgets 2.4.1.  However, since the proper solution
to the problem was never identified, it is possible that this problem will resurface with future
versions of wxWidgets.  It is also possible that this occurred as a bug in wxWidgets 2.4.0 that was
fixed in 2.4.1, in which case it's not likely to reappear.

The root of the problem is that wxWidgets was compiled without large file support enabled, while
Audacity was compiled with it.  This is determined by a #define symbol, _INPUT_BYTE_OFFSET being
set to 64 for large file support.  It should also affect libsndfile, but mysteriously it did not.
If you encounter this problem and find a fix, please submit a patch.

If you modify Audacity's Makefile and then run configure, configure will overwrite your changes
to the Makefile when it regenerates it.  This is not a bug, this is the expected behavior.  The way
to address this issue is to make sure that you migrate changes to the Makefile to Makefile.in
before rerunning configure.  This is also why it is recommended that you work with configure.in to
provide the features you need rather than editing Audacity's Makefile.  Just keep in mind that
Audacity's Makefile is a strong Makefile that successfully compiles under multiple Linux distributions,
and even FreeBSD.  If you find yourself modifying it a lot, you are probably making a mistake and
should step back and re-evaluate how you are working.  I was able to effect all changes needed
to successfully build Audacity under Cygwin without modifying Audacity's Makefile, although I
did frequently test changes by modifying the Makefile directly.  After each change, you should
find a way to put it in configure.in and rerun autoconf and configure.

----------
Known Bugs
----------

First of all, expect your resulting build to work differently in little ways than a build made with
MSVC++.

Here is a list of currently known bugs in Audacity's build and the resulting executable.  Any help
fixing these bugs would be greatly appreciated.

*  Zoom does not center around cursor
*  Audacity does not associate itself with filetypes  (It's #if !defined out, the code works in
MSVC++)
*  libsndfile fails to configure on some systems.  If this happens, the known workaround is to
configure libsndfile manually and then configure audacity again.
