Click to See Complete Forum and Search --> : Help with linking to static libraries


19bernardo87
October 3rd, 2008, 03:51 PM
Hi, I'm fairly new to API's (and programming in general) and recently started on windows. I'm using the Code::Blocks IDE.

I'm attempting to use the OPENFILENAME struct, unfortunately I get an Undefined Reference error when I try to use the function GetOpenFileName: it seems that I need to link with comdlg32.lib.

I do so by specifying in "Build Options"-"Linker Settings" and Adding "..\..\..\..\..\watcom-1.3\lib386\nt\comdlg32.lib" to "Link Libraries."

However I now get this error:
..\..\..\..\..\watcom-1.3\lib386\nt\comdlg32.lib: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 3 seconds)
0 errors, 0 warnings

Also, for learning purposes, I'd like to know if I understand this.

The reason I get undefined reference is because the header file <windows.h> only contains the declaration of the function, its definition lies inside of comdlg32.lib, which is a set of source of files put together more or less. Are these pre-compiled (already in object code, .o) and the linker just puts them together?

Notsosuperhero
October 3rd, 2008, 09:03 PM
Are you sure the file exists there? I haven't used Code::Blocks in a while so I'm not sure about the linker settings and stuff in C::B.
Are you using full path names, not just "\..\.."(like "C:\Whatever\Lib\file.lib")

Have you thought about switching to Visual C++ Express(it is free)? Just curious, as this would probably the best for Windows development.

And yes, the lib contains the actual implementation.

19bernardo87
October 4th, 2008, 10:09 AM
Notsosuperhero, problem somewhat solved.

I say somewhat because I got the program to run by linking to another comdlg32.lib, but now I'm getting a run time error on each call to GetOpenFileName and GetSaveFileName.

Here is the code:


case WM_COMMAND:
{
WORD command = LOWORD(wParam);

switch (command)
{
case IDM_MENU_NEWFILE:
{
HWND hEdit = GetDlgItem(hWnd,IDE_EDIT_MAINWND);

if (hEdit) SetWindowText(hEdit,"");

break;
}
case IDM_MENU_LOAD:
case IDM_MENU_SAVEAS:
{
OPENFILENAME ofn = {0};
TCHAR szFileName[MAX_PATH] = "";

ZeroMemory(&ofn,sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";

ofn.Flags = (command == IDM_MENU_LOAD) ?
OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY :
OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
/*
BOOL bOFN = (command == IDM_MENU_LOAD) ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);

if (bOFN)
{
}
*/
break;
}
}


Also, that's a fantastic tut on DLL. Do you know anything similar to SL?

Notsosuperhero
October 4th, 2008, 10:06 PM
Try setting the first byte of the lpstrFile in OPENFILENAME to 0


ofn.lpstrFile[0] = 0;


For some reason it does some funny things if its not set.

19bernardo87
October 4th, 2008, 11:52 PM
I "figured out" what the error was, which just comes to illustrate my lack of knowledge about static libraries.

.lib files are compiler specific, and Code::Blocks uses gcc.

At first I was linking to the watcom libraries, which gives me a linker error. I then changed to the one which comes with matlab, which uses a different compiler, this time giving me a run time error.

Finally I linked to the correct library. =)