Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Link Can't Find Function With Variable Arguments

Status
Not open for further replies.

Captrick458

Programmer
Mar 27, 2005
37
US
I am porting a library from Unix 'C' to MS VC++.

The linker can't find my function declared in the header file as:

int sprint(char *, char *, ...)

and called with something like

char ss[255];
sprint(ss,"%d",127);

The error message is:

testsprintDlg.obj : error LNK2001: unresolved external symbol "int __cdecl sprint(char *,char *,...)" (?sprint@@YAHPAD0ZZ)

BTW: other functions contained in the same LIB are found with no problem.

Any help will be greatly appreciated.
 
Erm, perhaps because it should be called [tt]sprintf[/tt]

--
 
Salem:

The short answer is, sprint() is correct.

The long answer, is that 30 years ago, we developed our own functions for writing to proprietary databases in Unix. The primary function was called dbfprint(). We liked the functionality and conventions better than the printf() family of functions, and developed our own for writing to strings, the screen, printer, files opened with fopen, etc.

In some cases, there is absolutely no similiar c++ function, and even where one exists, the conversion would be tedious, at best. So, even though the sprint() example is trivial, the underlying problem is not.

Any help will be greatly appreciated.

Rick
 
Put an extern "C" in front of the declaration. Cures all evils of mangled C++ names.
 
The extern "C" was an excellent idea, but it doesn't work.

Now the linker complains about its inability to find _sprint

Regards, Rick
 
Well you need to find what module or library the original "sprint" was implemented it, and port it across to your new environment.

As far as your new compiler is concerned, it's just another symbol you need to implement.

On second reading, I see...
> BTW: other functions contained in the same LIB are found with no problem.
But are any of those also variadic functions?

How did you create the new library - was it created by VC++ from source code?
If not, then you may have gotten lucky with the object code format, but come unstuck with some of the name mangling. Different C++ compilers mangle names differently.

--
 
We are creating, using MS VC++, the library containing sprint() from original source code modified to c++ calling conventions.

Other modules in the library are found.

Rick
 
Do you have a common header file, which is used when you built the library, and used in your code which uses that library?

> The extern "C" was an excellent idea, but it doesn't work.
Does that header file have the extern "C" bit?

Did you rebuild the library when you changed the external linkage in the shared header file?

--
 
You either need a definition file or a dll_export/import with the declaration. Definition files are easier if you do not wish to add Microsoftness to the source code.
 
Salem:

Yes I use a common header file, it is used in both source files, and the library is completely rebuilt after each change.

Rick
 
xwb:

Can you explain why, since both the library and the application are completely written, compiled, etc. in VC++.

Not trying to be argumentative, just don't understand.

Also, I looked in the MS documentation for DLL import/export files, and definition files, and they really don't seem to apply here. Can you be more specific---while I have 25+ years in 'C', I am VERY new to VC++.

Thanks for trying to help, Rick
 
MS libs are quite dumb: they do not export anything unless you tell them explicitly. It is not like Unix where everything is exported.

Are you creating a static library (*.lib similar to *.a in Unix) or a dynamic link library (*.dll similar to *.so/*.sl in Unix)? If it is a static library, it should link. If it is dynamic, create a new dll in visual studio and select the option "A DLL that exports some symbols". Have a look at the generated file. It is a template of what to do.

If you've done all that, in your link, you should specify the .lib which is generated for the .dll. Do not specify the .dll.
 
Guys:

Thanks for all the help.

I am feeling like a bit of an idiot right now. There were warnings about incompatable libraries, which I ignored because other functions were being found within the "incompatable library." I even created a function with a very unlikely name, and placed it in the library to make certain that the linker wasn't finding some similiar function in standard libraries.

I certainly can't explain why the other functions were found while "sprint()" was not, but once I corrected the problem that was creating the warning, sprint() was found using either c++ declarations or extern "C" declarations.

Thanks again for all the help!

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top