Build the TagLib library with Visual Studio 2010

From Bitpost wiki

NOTE that I've moved to Visual Studio 2013, and the updated instructions for that are here.




We'll use CMake to create a Visual Studio project file for the TagLib source so we can envoke the Visual Studio compiler. Weird, eh?

First, make sure you have Visual Studio 2010 and the Microsoft Windows SDK 7.1 installed. Then use the CMake Win32 Installer from here. You should click ‘add to PATH’ when installing CMake (thanks Steve!).

TagLib has Zlib as a dependency. Get the taglib and zlib source code and unzip/untar and move them around to set up this structure:

c:\taglib
c:\taglib\taglib
c:\taglib\zlib

Now paste this script (originally from here and tweaked) into c:\taglib\build_taglib.bat and run it:

rem MDM This is a nice build script for taglib that I found here:
rem MDM 	http://old.nabble.com/Cannot-compile-Taglib-for-x86-with-MSVC2010-td34048318.html#a34048899
rem MDM It pretty much does what we need out of the box.
rem MDM I fixed the tagd.lib copy line (it was using tag.lib before), that's about it.
rem MDM I am using the following tools and source files:
rem MDM
rem MDM		CMake windows installer cmake-2.8.9-win32-x86.exe
rem MDM		zlib 1.27 source
rem MDM		taglib 1.8 source
rem MDM
rem MDM Set things up here (don't try converting to deeper paths, they will fail):
rem MDM
rem MDM		c:\taglib\taglib
rem MDM		c:\taglib\zlib
rem MDM		c:\taglib\build_taglib.com (this file)
rem MDM

set ProgFilesRoot=%ProgramFiles%
set ProgFiles86Root=%ProgramFiles(x86)%
if not "%ProgFiles86Root%"=="" GOTO 64bitWindows
set ProgFiles86Root=%ProgramFiles%
:64bitWindows

set 64bitcl=true

if not exist "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\amd64\cl.exe" goto 32bitcl
set 64bitcl=false
:32bitcl


rem ######## zlib
cd zlib\

rem #Release x86
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Release /x86 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF .
nmake clean
nmake
mkdir .\Release\x86
move /Y zlib.lib .\Release\x86\zlib.lib

if "64bitcl"=="false" goto taglibrelease

rem #Release x64
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Release /x64 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF .
nmake clean
nmake
mkdir .\Release\x64
move /Y zlib.lib .\Release\x64\zlib.lib

cd ..\


:taglibrelease

rem ######## Taglib
cd taglib\

rem #Release x86
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Release /x86 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DZLIB_INCLUDE_DIR=..\zlib\ -DZLIB_LIBRARY=..\Release\x86\zlib.lib -DENABLE_STATIC=1 -DWITH_MP4=1 -DWITH_MP4=1 .
nmake clean
nmake
mkdir .\taglib\Release\x86
move /Y .\taglib\tag.lib .\taglib\Release\x86\tag.lib

if "64bitcl"=="false" goto taglibdebug

rem #Release x64
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Release /x64 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DZLIB_INCLUDE_DIR=..\zlib\ -DZLIB_LIBRARY=..\Release\x64\zlib.lib -DENABLE_STATIC=1 -DWITH_MP4=1 -DWITH_MP4=1 .
nmake clean
nmake
mkdir .\taglib\Release\x64
move /Y .\taglib\tag.lib .\taglib\Release\x64\tag.lib

:taglibdebug

rem #Debug x86
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Debug /x86 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DZLIB_INCLUDE_DIR=..\zlib\ -DZLIB_LIBRARY=..\Release\x86\zlib.lib -DENABLE_STATIC=1 -DWITH_MP4=1 -DWITH_MP4=1 .
nmake clean
nmake
mkdir .\taglib\Debug\x86
rem MDM tagd.lib -> tag.lib
move /Y .\taglib\tag.lib .\taglib\Debug\x86\tag.lib
move /Y .\taglib\tag.pdb .\taglib\Debug\x86\tag.pdb

if "64bitcl"=="false" goto done

rem #Debug x64
del ".\CMakeCache.txt"
call "%ProgFilesRoot%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /Debug /x64 /win7
call "%ProgFiles86Root%\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\vcvarsx86_amd64.bat"
"%ProgFiles86Root%\CMake 2.8\bin\cmake" -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DZLIB_INCLUDE_DIR=..\zlib\ -DZLIB_LIBRARY=..\Release\x64\zlib.lib -DENABLE_STATIC=1 -DWITH_MP4=1 -DWITH_MP4=1 .
nmake clean
nmake
mkdir .\taglib\Debug\x64
rem MDM tagd.lib -> tag.lib
move /Y .\taglib\tag.lib .\taglib\Debug\x64\tag.lib
move /Y .\taglib\tag.pdb .\taglib\Debug\x64\tag.pdb

:done

cd ..\

rem MDM wow did that just work?
pause

Now set up your project to use it!

I'm using Qt. First, add the include and lib paths to your Qt project .pro file:

win32 {
	CONFIG(Debug, Debug|Release) {
		win32:LIBS += "C:/taglib/taglib/taglib/Debug/x86/tag.lib"
	} else {
		win32:LIBS += "C:/taglib/taglib/taglib/Release/x86/tag.lib"
	}
}
# MDM For taglib headers
win32:INCLUDEPATH += "C:/taglib/taglib/taglib/Headers"

Then (re-)create your VS project if needed by opening the .pro file with the Qt Visual Studio Add-in.

Now include the header right before you use it - don't forget the #define or it won't be happy:

#define TAGLIB_STATIC
#include "taglib.h"
#include "mpegheader.h"
#include "mpegfile.h"

TagLib::MPEG::File file(str_song_filename.toStdString().c_str()); 
file.strip();
file.save();
// etc. or whatever...

Now build it on up! Have fun with it!