1

I am using VS2010 and I have a C++ project that is referencing and using an external C library (dll) by having various entries in the VC++ Directories and Linker sections of the project properties.

Right now my project is building but when it starts, a message box appears :

The program can't start because ExternalCLibrary.dll is missing from your computer. [...]


I would like to know how to do in Visual Studio 2010 the the equivalent of

adding "-static-libgcc -static-libstdc++" to your compiler flags.

It seems to be the solution according to: The program can't start because libgcc_s_dw2-1.dll is missing

Community
  • 1
  • 1
Bruno
  • 4,685
  • 7
  • 54
  • 105
  • @MichaelPrice: sorry, clarified :p – Bruno Nov 11 '11 at 22:56
  • So, where is this dll? If it is not in your .exe directory then Windows has very small odds finding it. The project properties don't help, they are only used at build time, not run time. – Hans Passant Nov 11 '11 at 23:01
  • @HansPassant: The dll it is in /lib directory, relative to the exe. How can I specify this to my program? I don't want it in the same exe's directory... – Bruno Nov 11 '11 at 23:08

4 Answers4

3
  1. Load your project in Visual Studio.
  2. Right click your project and choose Properties.
  3. Locate the "Linker" portion of the tree on the left.
  4. Choose All Configurations and All Platforms from the drop down menus at the top of the dialog.
  5. Put your additional static library dependencies in the Input -> Additional Dependencies field, semicolon delimited.
  6. If the libs are not on your lib search path, make appropriate entries in the General -> Additional Library Directories field, semicolon delimited.
  7. Apply, save, compile, run.
Michael Price
  • 8,088
  • 1
  • 17
  • 24
  • it seems there is only 'Additional Libraries' and no 'Additional Library Directories' field? http://i.imgur.com/Oqx9R.png – Bruno Nov 11 '11 at 23:01
  • @ibiza Sorry... some names were wrong as I was typing from memory... editing now. – Michael Price Nov 11 '11 at 23:02
  • 1
    Also, these are the instructions for adding a dependency on a library. If that static .lib is paired with a .dll, the .dll will have to be on your runtime executable search path (most likely should be in the same directory as the executable). – Michael Price Nov 11 '11 at 23:05
  • Hi and thanks, I have already all of these settings, but somehow it still seems to try to use the dll? Thinking of it, maybe it's because I filled the Reference Directories in the [Configuration Properties -> VC++ Directories] section? edit: just removed the path for References and it changed nothing – Bruno Nov 11 '11 at 23:06
  • My guess is that the .lib you are linking against is just an import library for the DLL in question. By linking against it, you are requiring that the DLL be loaded into your application at executable load time (instead of delay loading via LoadLibrary). See http://msdn.microsoft.com/en-us/library/windows/desktop/ms686923(v=VS.85).aspx – Michael Price Nov 11 '11 at 23:18
0

This process is simple, however you need to be aware of several things. The first thing, if your lib is written in C, in the header files of every source containing C functions us the following.

#ifdef __cplusplus
extern "C" {
#endif

// C functions

#ifdef __cplusplus
}
#endif

Once you've done that, compile the library into a static library using the ar rcs [YOUR OBJECT FILES]. The final thing to do is us the c++ compiler link the library with the object files from your project. Now flags are required to link the library.

0

You can't use dll as a static library ( that's why they are called Dynamic-link library ). In order to compile a static library, you'll the source code of that library. Once you have the source code, go to Project settings, General->Configuration Type and set it to Static Library(.lib). Then in your program, you'll need to add that library by putting the library name in Linker->Input->Additional Dependencies

JosephH
  • 8,465
  • 4
  • 34
  • 62
  • okay, I already did that too, I have both dll and library correct settings in my project properties. How can I indicate to my program to use the static versions? – Bruno Nov 11 '11 at 23:03
  • You don't need to. Just include that library name in `Linker->Input->Additional Dependencies` and include the header files, and use the functions. – JosephH Nov 11 '11 at 23:05
  • Refer to http://msdn.microsoft.com/en-us/library/ms235627.aspx for the complete steps – JosephH Nov 11 '11 at 23:06
  • hmm...well I have all these settings correct and it still wants the dynamic dll file :( – Bruno Nov 11 '11 at 23:10
  • Be sure to read the documentation of the external library that you're using. Libraries will have different function declarations for dynamic libraries and static libraries. For example, if you want to link Curl statically, you **need** to define `CURL_STATICLIB` or `XERCES_STATIC_LIBRARY` for apache xerces etc. – JosephH Nov 11 '11 at 23:13
0

The two flags passed to gcc as per your question tell gcc to link the runtime library statically to an executable or shared library/dll. This is unlikely to be the problem with your issue as the part of the error message you quoted suggests that ExternalCLibrary.dll isn't being built properly.

If the DLL exists, use a tool like dependency walker to determine which dependency of your DLL can't be loaded; that's the likely culprit.

If ExternalCLibrary.dll doesn't exist then you need to find out where you are supposed to get it from, but if your project builds and it's listed in the project as a dependency then my guess is that it's an issue with the loader not being able to find a dependency of this DLL at runtime.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
  • Hi and thanks, ExternalCLibrary.dll is built correctly as if I add the path where my dll is to the PATH environment variable, the whole application works. – Bruno Nov 11 '11 at 23:09
  • That would make sense, because the loader checks the path variable for DLLs that aren't in the current directory. You might want to consider configuring your project so all dependent DLLs end up in the same directory as the main application to avoid this issue. – Timo Geusch Nov 12 '11 at 00:38