www.digitalmars.com         C & C++   DMDScript  

c++ - snprintf linker error

reply GreatEmerald <pastas4 gmail.com> writes:
I am trying to compile a very simple test program that uses the
snprintf() function with DMC, and it returns an error:

C:\project>..\dm\bin\dmc.exe debug.c
link debug,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 8.00.5
Copyright (C) Digital Mars 1989-2009  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
debug.obj(debug)
 Error 42: Symbol Undefined _snprintf

--- errorlevel 1

However, it should clearly be included in Stdio.h. So how come it's
not working as expected? Using Windows XP 32-bit. For reference, the
C file was as follows:

#include <stdio.h>

int main()
{
    char str[80];

    snprintf(str, 80, "%d", 5);
    printf(str);
    return 0;
}
Mar 09 2011
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/9/2011 5:31 AM, GreatEmerald wrote:
 I am trying to compile a very simple test program that uses the
 snprintf() function with DMC, and it returns an error:

 C:\project>..\dm\bin\dmc.exe debug.c
 link debug,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 8.00.5
 Copyright (C) Digital Mars 1989-2009  All rights reserved.
 http://www.digitalmars.com/ctg/optlink.html
 debug.obj(debug)
   Error 42: Symbol Undefined _snprintf

 --- errorlevel 1

 However, it should clearly be included in Stdio.h. So how come it's
 not working as expected? Using Windows XP 32-bit. For reference, the
 C file was as follows:

 #include<stdio.h>

 int main()
 {
      char str[80];

      snprintf(str, 80, "%d", 5);
      printf(str);
      return 0;
 }

snprintf isn't in stdio.h, use _snprintf instead.
Mar 09 2011
parent reply GreatEmerald <pastas4 gmail.com> writes:
Oh, I see. Does the same work with ShellExecute() as well?

On a related note, I've noticed that functions declared as inline, like this,
also
give me the same linker error:

inline void UpdateScreen()
{
        SDL_UpdateRect(GfxData[SCREEN],0,0,0,0);
}

If I simply make it non-inline, it compiles just fine. So why is that?
Mar 10 2011
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/10/2011 3:53 AM, GreatEmerald wrote:
 Oh, I see. Does the same work with ShellExecute() as well?

 On a related note, I've noticed that functions declared as inline, like this,
also
 give me the same linker error:

 inline void UpdateScreen()
 {
          SDL_UpdateRect(GfxData[SCREEN],0,0,0,0);
 }

 If I simply make it non-inline, it compiles just fine. So why is that?

When the linker says a symbol is undefined, that means it is not in the runtime library.
Mar 11 2011
parent reply GreatEmerald <pastas4 gmail.com> writes:
Yes, but how can it not be there in my case with UpdateScreen()? Does the
compiler
somehow make it so that every UpdateScreen() call is replaced by the text inside
the function plus an additional UpdateScreen() call that fails or something?..
Or
maybe it should be set like in VC++, __inline instead of inline?

Oh, and about ShellExecute(), the error actually mentions ShellExecuteA, and the
code that causes it is this:

#ifdef WIN32
#include <windows.h>
void OpenWebLink(char *web)
{
        ShellExecute(0,"open",web,NULL,NULL,SW_SHOWNORMAL);
}
#endif

Yet in SHELLAPI.H, which is included in WINDOWS.H, we see this:

WINSHELLAPI HINSTANCE APIENTRY ShellExecuteA(HWND hwnd, LPCSTR lpOperation,
LPCSTR
lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd);
WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
#ifdef UNICODE
#define ShellExecute  ShellExecuteW
#else
#define ShellExecute  ShellExecuteA
#endif // !UNICODE
Mar 11 2011
parent Walter Bright <newshound2 digitalmars.com> writes:
On 3/11/2011 2:43 PM, GreatEmerald wrote:
 Yes, but how can it not be there in my case with UpdateScreen()? Does the
compiler
 somehow make it so that every UpdateScreen() call is replaced by the text
inside
 the function plus an additional UpdateScreen() call that fails or something?..
Or
 maybe it should be set like in VC++, __inline instead of inline?

 Oh, and about ShellExecute(), the error actually mentions ShellExecuteA, and
the
 code that causes it is this:

 #ifdef WIN32
 #include<windows.h>
 void OpenWebLink(char *web)
 {
          ShellExecute(0,"open",web,NULL,NULL,SW_SHOWNORMAL);
 }
 #endif

 Yet in SHELLAPI.H, which is included in WINDOWS.H, we see this:

 WINSHELLAPI HINSTANCE APIENTRY ShellExecuteA(HWND hwnd, LPCSTR lpOperation,
LPCSTR
 lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd);
 WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
 LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
 #ifdef UNICODE
 #define ShellExecute  ShellExecuteW
 #else
 #define ShellExecute  ShellExecuteA
 #endif // !UNICODE

I'm not sure how to explain this, but if the linker says the symbol is undefined, then it is not in the library. A couple things that may be helpful: 1. You can compile with -e -l. This will create a file.lst, which shows the results of all macro expansions. This helps in figuring out just what the compiler is seeing. 2. Run obj2asm on the .obj file output by the compiler. This will show you exactly what symbols are referenced by the object file. Those references must be in the library, or you will get a symbol undefined error from the linker.
Mar 12 2011