views:

260

answers:

2

I'd like to create a static library using windows tools (cl.exe, link.exe, lib.exe, etc.) but I don't want to manually enter the names of the functions to export either in a .def file or specifying __declspec(dllexport) in the file. It should be similar to creating a .a file on linux, where anything not static to the files the library was created from is exported. Is there a way to do this?

A: 

You don't need __declspec(dllexport) to build a static library. Visual C++ exports symbols by default for static builds.

I just tried it with a trivial static library consisting of only:

void exportme_cpp() { }
extern "C" void exportme_c() { }

and it generates a .lib file containing:

?exportme_cpp@@YAXXZ
_exportme_c

Update, here's a complete listing that works:

testlib.cpp:

extern "C" void exportme_c() { }
void exportme_cpp() { }

testapp.cpp:

extern "C" void exportme_c();
void exportme_cpp();

int main(int, char*[])
{
    exportme_c();
    exportme_cpp();
}

To build:

cl -c testlib.cpp
lib testlib.obj -OUT:testlib.lib
cl testapp.cpp testlib.lib

Obviously you could avoid duplicating the function declarations by placing them in a header file included by both the library code and the code linking to the library.

Tim Sylvester
How do you build this on the command line? When i tried with the command: lib foo.obj -OUT:foo.libthen tried to compile: cl yup.c foo.libyup.c wasnt able to reference any of the functions in the foo.lib.
Jake
Did you get a compile error or a link error? You'll need to declare the functions in the dependent code, e.g., `void foo();`, before you can use them.
Tim Sylvester
A: 

Sorry about this question, I accidentally compiled the program wrong. The build process is a bit complicated at my company and its easy to overlook something.

Jake