I am trying to write DLL files under the Digital Mars C/C++ compiler for use with Liberty BASIC, but I can't seem to get them to compile correctly. Can anyone help me with what I'm doing wrong?
Printable View
I am trying to write DLL files under the Digital Mars C/C++ compiler for use with Liberty BASIC, but I can't seem to get them to compile correctly. Can anyone help me with what I'm doing wrong?
I don't really understand how you want the code placed in the window. I was confused by the instructions, so if you could offer a better explanation, that would help me greatly. :(
I get a ton of error messages, saying things like "Symbol Undefined." I can make DLLs with functions that don't use <stdio.h> or other headers, but when I try to include header files, it doesnt work right.Code:#include <ben.h>
#include <stdio.h>
unsigned int file_func(char dir[], char dir2[])
{
FILE *fp, *fp2;
fp = fopen(dir, "r");
fp2 = fopen(dir2, "w");
fclose(fp);
fclose(fp2);
return 0;
}
here is my header code for <ben.h>:
Code:#ifndef BEN_H
#define BEN_H 1
unsigned int file_func(char dir[], char dir2[]);
#endif
You need to post the actual error messages.
I'll have to look at them. I'll have them posted sometime soon
this code actually works:
this code doesn't, and has these error messages:Code:#include <dlldemo.h>
unsigned int foo(unsigned int i)
{
return i / 2;
}
unsigned int bar(unsigned int i)
{
return i / 3;
}
Error 42: Symbol Undefined _GetCurrentThreadId@0Code:#include <dll.h>
unsigned int filecopy(FILE *ipf, FILE *opf)
{
int c;
while ((c = getc(ipf)) != EOF)
putc(c, opf);
}
Error 42: Symbol Undefined _CreateSemaphoreA@16
Error 42: Symbol Undefined _ReleaseSemaphore@12
and the list goes on. Any idea what Im doing wrong?
Make sure you have <stdio.h>, directly or indirectly.
What are you linking with? Why do you have <dll.h> instead of <dlldemo.h>?
<dlldemo.h> was a library that I got from a website, and <dll.h> was a library of mine to prototype the function. Here is the code for both of them:
dlldemo.h:
dll.h:Code:#ifndef DLLDEMO_H
#define DLLDEMO_H 1
unsigned int foo(unsigned int i);
unsigned int bar(unsigned int i);
#endif
My compiler does everything on its own. All I do is type "dmc dlldemo.c dlldemo.dll" and it compilesCode:#ifndef _DLL_H_
#define _DLL_H_
#include <stdio.h>
unsigned int filecopy(FILE *ipf, FILE *opf);
#endif
Other than the fact that you don't return a value and you don't include <stdio.h> (which is where FILE is defined), there's nothing wrong with this code. Nothing that should be producing linker errors. Try the code (with a void return value) in a separate program.Code:unsigned int filecopy(FILE *ipf, FILE *opf)
{
int c;
while ((c = getc(ipf)) != EOF)
putc(c, opf);
}
I've used this code before, and it always worked perfectly, but when I try to make it into a DLL file, I get those error messages. I dont understand what I'm doing wrong. Can you maybe explain how I'd go about making these into a DLL file? I just forgot that I changed the return type, so I'll cange it back.
also, my source file uses <dll.h>, which includes <stdio.h>
I'm getting 'Symbol Undefined' messages while linking
The FAQ: http://www.digitalmars.com/faq.htmlQuote:
Symbol Undefined
A symbol remained undefined after all input files, including libraries, had been processed. Common causes for this are:
* A function was called in your code, but the function was never written.
* A virtual function was declared, but never written.
* A data variable was referenced, but never defined anywhere.
* Did not specify all the .obj files to the linker.
* The calling conventions of the function being referenced do not match the calling conventions of the defined function. Compiler switches, memory models, and special keywords can all affect calling convention (and thereby the name of the symbol as it appears in the .obj file).
* One or more missing library files (.lib). One way to figure out which .lib file contains the missing symbol is to run:
\dm\bin\grep symbolname \dm\lib\*.*
* The LIB environment variable is missing or not pointing to the \dm\lib directory or wherever the .lib files reside.
well, what i cant understand is why it allows me to make some DLLs, but not others. I'm not using any of the symbols or anything that it is saying... any idea as to why it compiles code that has no header files included, but not ones that do?
You need to link to kernel32.dll, and that should get rid of those errors. Read the digitalmars FAQ that Tonto linked to in his last post to figure out how to link to libraries. The import library name (assuming your compiler can link to import libraries) is kernel32.lib
thanks, I'll try that out
ok, here's some more info: listed above the error messages is this:
C:\dmc\dm\bin\..\lib\SNN.lib(thsup)
Error 42: Symbol Undefined _GetCurrentThreadId@0
C:\dmc\dm\bin\..\lib\SNN.lib(semlock)
Error 42: Symbol Undefined _CreateSemaphoreA@16
C:\dmc\dm\bin\..\lib\SNN.lib(semlock)
Error 42: Symbol Undefined _ReleaseSemaphore@12
and the list goes on. still the same advice?
Yes. Those 3 functions are all defined in kernel32.dll.
usually, I don't have problems with crap like this, but i cant remember where kernel32.dll is. :o
I found it!
I'm not sure if i made this clear enough. I'm not trying to write a program using code from a DLL, I'm trying to write my own DLL for a BASIC App. that I'm working on. I want to write it, because im hoping that a C function will be faster than a BASIC function. any advice?
On how to write your own DLL?
yes. absolutely. thats what ive been trying to do this whole time... any clue what I'm supposed to do?
This depends on your compiler. I've never used the digitalmars compiler before, so I can't help you. I suggest reading your compiler's documentation and help files.
Typically, you need to tell your compiler which functions are being exported. This is done with a .DEF file, or a keyword in the exported function's signature (like __declspec(dllexport) on MSVC compilers). Lastly, there is some directive that you give to the linker which tells it you are creating a DLL file instead of a normal executable.
yeah, i knew that I had to use the commandline switch -WD, but as for the definitions file, do I link to the def file or what?
yes, you do.
do i link the a library to the .def file, or do I link the program to the .def file? Can you maybe give me an example program and def file, so that i can see what i need to do?
That question doesn't really make sense. You pass the DEF file to the linker during the linking stage. On MSVC this is done by "/def myfile.def".
Here is an example of a .DEF file:
And your code:Code:; myfile.def : exported library symbols
LIBRARY "mylib"
DESCRIPTION 'mylib Windows Dynamic Link Library'
EXPORTS
foo
bar
Code:int __stdcall foo(void)
{
return 1;
}
void __stdcall bar(int a)
{
a++;
}
well, my compiler preprocesses, links, and compiles all on its own. do i need to link the def file to my program, or to a library file, such as the above kernel32.lib?
do i need to declare a main() function in the program? the DLL compiles right when i do, but i dont understand how i would call the function in the DLL file... this is alot more complicated than it needs to be.... :(
You link the def file to your library. You are creating a DLL file. The def file is part of your DLL project. The def file is what tell the linker what functions are exported.Quote:
well, my compiler preprocesses, links, and compiles all on its own. do i need to link the def file to my program, or to a library file, such as the above kernel32.lib?
No offense, but this is only complicated because you apparently have no idea what a DLL file is, or what it does. DLL files don't have main functions because they are not programs. They are just function libraries. You call your DLL functions from your program by linking to your DLL file. Do a google search on "DLL Tutorial" to read more. You are undoubtably going to have questions on how to link to your DLL and call it's functions, and there is no quick answer for that. You need to read some tutorials to learn the general basics of what DLL files are for, and how applications link to them. Then when you have more specific questions, post them here.Quote:
do i need to declare a main() function in the program? the DLL compiles right when i do, but i dont understand how i would call the function in the DLL file... this is alot more complicated than it needs to be....