Thursday, 15 January 2015

Building 3D Studio File Toolkit in VS2013

I’ll preface this post with the disclaimer that I am only a novice in C/C++ programming and using Visual Studio.

In the course of building TRViewer [link] the linker gave an error about not being able to open LIBC.lib.

LIBC is an old version of the Microsoft C runtime that was removed in Visual Studio 2005.

TRViewer is a Visual Studio 2003 project and so the third party libraries it uses from that era may use old Visual Studio default libraries.

It was found that the library requiring LIBC was the 3D Studio File Toolkit, ftkvc40.lib which had presumably been built in Visual C 4.0.

3D Studio File Toolkit is used to manage the 3D modelling format, *.3DS.

The quick fix was to set the linker to ignore LIBC using the /NODEFAULTLIB option.
According to posts found on the internet [link1, link2, link3] the proper thing to do is to recompile the library dependent on LIBC in the same version of Visual Studio in which you are building your program.

The rebuilt library must also use the same C runtime as your program.

The C runtime that a program uses is set in the project’s property pages.

See Configuration Properties>C/C++>Code Generation>Runtime Library where you have the choice of four runtimes, /MT, /MTd, /MD or /MDd.

Four runtimes

/MT and /MTd link to static runtimes and /MD and /MDd link to DLL runtimes.

The first step to rebuilding the 3D Studio File Toolkit is to obtain the source code which is not included in the TRViewer sources.

An internet search for “3dsftk.zip” finds it, so download and extract it.

Important: The root folder of 3dsftk I extracted showed some folders were read only. Make all subfolders read/write by unchecking the Read-only box in the properties for the root folder.

Make 3dsftk folders writable

The SOURCE folder of the zip does not contain a Visual Studio solution, workspace or project but instead contains a makefile, “makefile.vc4”.

This means the library was built from the command line using Visual Studio’s nmake tool.

Since I do not yet know how to make a Visual Project using the settings specified in the makefile I will try and use the nmake command line tool to create the library.

You run nmake not from the Windows command prompt but from Visual Studio’s Developer Command Prompt.

When you installed Visual Studio it should have created a Start Menu shortcut, “Visual Studio Tools”.

Visual Studio Tools shortcut

Click the Visual Studio Tools shortcut and double click on the “Developer Command Prompt for VS2013”.

Developer Command Prompt shortcut

Developer Command Prompt

To build the library, in the command prompt, you navigate to the SOURCE folder and use the nmake command.

Before reading any documentation for nmake you can find what nmake command to use in the *.bat files contained in the 3dsftk root folder.

MAKEDBG.BAT contains the command, “nmake DEBUG=1 -f makefile.vc4”, so that is the command to make the debug version of the library.

MAKEDBG.BAT contains the command, “nmake -f makefile.vc4”, so that is the command to make the release version of the library.

CLEAN.BAT contains the command, “nmake -f makefile.vc4 clean”, so that is the command to clean the folders. Clean means delete the compiled object files so that the compiler is forced to recreate them next run.

As a word of warning, before running the nmake command you should examine the makefile and the source code to see that there is nothing malicious in them.

In practice this means you need a solid understanding of nmake makefiles, C/C++ and the compiling and linking process but as beginners all we can do is take a few precautions like having a system backup or possibly using a virtual machine, not using a PC that is critical and having up to date virus and malware detection programs  running.

So understanding the risk, navigate to the SOURCE folder using the command prompt.

I find it easiest to open the folder in a File Explorer window, copying the address by right clicking on the address bar, typing “cd “ in the command prompt, pasting the copied address by right clicking on the title bar of the command prompt window and selecting Edit>Paste and then pressing the enter key.

At the command prompt type “nmake DEBUG=1 -f makefile.vc4” and press enter.

Build fails

As expected the build fails, time to get some documentation about nmake makefiles.

Using the reference found here [link] modify makefile.vc4 as shown below.


The debug version of TRViewer uses the /MDd runtime so I added this option to the debug compiler flags in the makefile.

The release version of TRViewer uses the /MT runtime so I added this option to the release compiler flags in the makefile.

If I understand correctly, as well as creating the library, the makefile creates two executables.

The scanhdr.exe program is used on all the separate object files to create the include file 3dsftk.h for the library.

The other executable creates the swapbyte.c file.

Replace makefile.vc4 with the modified version and at the command prompt run the command “nmake – f makefile.vc4 clean” just to be sure there are no object files left over from the last attempt.

Clean the project

At the command prompt run the command “nmake DEBUG=1 –f makefile.vc4” to build the debug version of the library.

Debug library builds with warnings

Run the clean command again just to be sure we use fresh object files and then run the command “nmake –f makefile.vc4” to build the release version of the library.

Run clean

Release version builds with warnings

The new versions of the library are found in the LIB folder and the header file is found in the INC folder. Notice that the new libraries are bigger than the old ones.

New libraries

With the new libraries added to the project and the ignore LIBC option removed, TRViewer links without error but it remains to be seen whether the *.3ds import and export functions operate correctly.

Next thing to learn is how to create a *.dll for the 3DS File Toolkit so it can be used in other programs written in languages other than C/C++.

1 comment:

  1. Thank you so much for posting this! I was completely lost with this until I came across this.

    ReplyDelete