I am trying to create my own DLL and then make another project load it statically.
My DLL file contains both a header file (called HelloFunc.h):
#include <stdio.h>
extern "C"
{
_declspec(dllexport) void HelloFromDll();
}
And a c file (called HelloFunc.cpp):
#include <stdio.h>
extern "C"
{
_declspec(dllexport) void HelloFromDll()
{
printf("Hello DLL. \n");
}
}
After building the project an Object File Library (.lib) was created.
Then, on my other project I tried to link to it statically.
In linker -> Input -> Additional Dependencies I added my library (I put it in my new project's directory) and then in linker -> Input -> Command Line I saw that it actually linked to it.
However, when I tried to call HelloFromDll() function in my new code, an error says that it is not identified. Note that I also included "HelloFunc.h" but an error says that the source file couldn't be opened.
I'm a little lost and don't know what I've done wrong. Any help will be appreciated :)
You must specify __declspec(dllimport) instead of __declspec(dllexport) when importing a library.
What error message did you received exactly?
[Edited]
When you compile a DLL, you specify __declspec(dllexport). When you compile an application that imports the DLL, you specify __declspec(dllimport).
The problem is that the compiler cannot find HelloFunc.h: Simply copy HelloFunc.h into your new project's directory.
Check that you client & library were compiled in the same mode (debug or release).
This is common bottleneck.
Related
Long story short, I want to call C code from Rust... I've seen crates like cc and cmake which (to my understanding, which might be wrong) can compile C code for use with the Rust language. Though I am capable of producing a compiled .lib file using the CLion IDE. Now I would like to simply link to said .lib file from Rust and just call the code within, is this possible? I have tried a few things, but they just do not seem to link at all...
project structure:
my_rust_project/
my_c_project/
cmake_build_debug/
...
my_c_project.lib
...
...
a_c_file.h
a_c_file.c
...
src/
main.rs
cargo.roml
build.rs
...
build.rs:
fn main() {
println!("cargo:rustc-link-search=my_c_project/cmake-build-debug/");
println!("cargo:rustc-link-lib=static=my_c_project/cmake-build-debug/my_c_project");
}
main.rs:
#[link(name = "my_c_project/cmake-build-debug/my_c_project", kind = "static")]
extern "C" {
fn hello();
}
fn main() {
unsafe {
//hello(); /* if commented the project compiles... */
}
}
Note how you could comment out any of the two prinln!'s in build.rs as well as the #link directive from main.rs in any combination you could think of (i.e. commenting out #link and leaving the println's, or commenting out one println and leaving the #link, and so on...) and the project will continue to compile, though as soon as you uncomment hello(); from fn main() in main.rs, the project will not compile and produce the following error:
LNK2019: unresolved external symbol hello referenced in function blah blah blah ...
I think there is two possible causes, either the library is not link at all, which seems to be the most logical issue to me, which raises the question: what exactly do the println!'s and #link do in this specific scenarion, they do not cause a panic! or error so they must definitly be doing something right? Whatever it is, it cannot be linking the project.
The other issue I can imagine is that there is in fact proper linking going on-ish? But Rust just doesn't understand where exactly to find the function hello within my_c_project.lib...
For clarity I will provide the contents of my_c_project:
a_c_file.h:
#ifndef MY_C_PROJECT_A_C_FILE_H
#define MY_C_PROJECT_A_C_FILE_H
void hello();
#endif //MY_C_PROJECT_A_C_FILE_H
a_c_file.c:
#include "a_c_file.h"
#include <stdio.h>
void hello() {
printf("Hello, World!\n");
}
I really want to stress that I want to stay away from cc and cmake (crates) since I have the .lib file ready to go... The question remains, how to call the C functions from a .lib file from within a Rust program?
Thanks for the help!
newly tried thing...
changed build.rs to:
fn main() {
println!("cargo:rustc-link-search=my_c_project/cmake-build-debug/");
println!("cargo:rustc-link-lib=static=my_c_project");
}
changed main.rs to:
extern {
fn hello();
}
fn main {
}
renamed my_c_project.lib to libmy_c_project.lib...
This still produces an error:
LINK : fatal error LNK1181: cannot open input file 'libmy_library.lib'
I guess it does not require a .a file per se as it says .lib in the message above.
If anyone ever has this problem... I solved this by continuing the project on Linux using cross compilers to generate the executable for windows.
The matter is to compile everything (source code and libraries/dependencies) for the target platform using the correct compilers ofcourse.
I am just starting out with Segger Embedded Studio. Right now trying to call a function of a included header file. The file seems to be included since it shows up in the dependencies. For now I simply included the header and C-File in the project directory.
The included Header- and C-Files are simply:
//##### Header-File ######
#ifndef TEST_H_
#define TEST_H_
void printText(void);
#endif //TEST_H_
and:
//###### C-File #########
#include <test.h>
#include <stdio.h>
#include <stdlib.h>
void printText(void)
{
printf("Hello");
}
But when I try to call the printText Function in my main I get the error:
"Undefined Symbol: printText".
Why is the function not recognized?
"Undefined symbol" is a linker error. You are not linking the object code containing the definition of printText().
It is not an issue with the header file; including a header file does not cause the associated code to be linked - that is just the declaration so the compiler knows what the interface looks like. It is the linker than combines the separately compiled object code to form a program. You have not told the linker to use the object code containing printText(), and you have not told your IDE project to compile it to generate that object code.
The project tree clearly shows that only main.c is included in your project; you need to add the C file containing printText() too.
The concept of separate compilation and linking is what you need to grasp here.
Thank you Clifford for your answer. You are right I had some miss-conceptions about which files will be linked while building the project. In the special case of segger embedded studio there are, as I know by now, two ways to reference extern files.
Adding the files to the sources files folder is straight forward, but must be done separately for each project you want to use the respective files.
For frequently used files it is beneficial to create a dedicated library solution. Such a library can then be imported to any solution by choosing "add existing project". This will add all files of the library to your current solution (and show them in the project-tree). Now click the tab project -> dependencies. Your library should show up here. By acitivating the check box the linker will compile the referenced project upon builing your solution, allowing for the usage of your library functions.
Adding and linking library-project to a current solution
I'm writing a DLL for an existing application. The DLL is designed to interface to commercial software who's functionality can be enhanced by adding a user defined feature with a DLL.
My DLL compiles and the application can utilize the features, but I want to debug it. It is difficult for me to attach to the process and debug when the commercial software is calling the DLL. Instead, I'd like to build separate code to test the DLL.
All the examples I can find w/DLL's are for C# or C++. I can't quite understand how to do this in C. I seem to be stuck at importing the DLL or referencing the library in the linker.
I've inserted:
__declspec(dllimport) int UserDefinedSurface4(USER_DATA *UD, FIXED_DATA4 *FD);
but I get:
LNK2019 unresolved external symbol "__declspec(dllimport) int __cdecl UserDefinedSurface4(struct USER_DATA *,struct FIXED_DATA4 *)" (__imp_?UserDefinedSurface4##YAHPEAUUSER_DATA##PEAUFIXED_DATA4###Z)
Following advice for C# and C++, I've added a path to the DLL in my project using: Properties->Linker->Input->Additional Dependencies with an explicit path: "D:......\mydll.lib" (that seems kind of brittle... is this right or should I use a relative path or ...?)
Furthermore, I also inserted:
#pragma comment(lib, "D:\\...\\mydll.lib")
and I have included the project with the DLL in my references. What's missing?
Your dll import looks fine.
Check the name of the exported function. Good tool for this task is "Dependency Walker". If your dll compiled as C++, function name can be mangled. To prevent name mangling your export definition in DLL project should be:
extern "C" __declspec(dllexport) int UserDefinedSurface4(USER_DATA *UD, FIXED_DATA4 *FD);
In your application specify Linker -> Additional Library Directories to your library directory and in Linker -> Input specify additional dependency mydll.lib. After that check Linker -> Command Line parameter, it should contains:
"mydll.lib" /LIBPATH:"d:\yourlibrarydir\"
Context:
I would like to add a custom library to a piece of Arduino code in the Arduino 1.5.7 IDE to ensure code is decentralized and readable
Attempted solution:
I make a folder called "mathsfunctions". In it I put two text files, one with a .c and another with a .h name extension.
The .c file is called "mathsfunctions.c" and has the following code in it:
#include "mathsfunctions.h"
int multiply (int a, int b)
{
return a*b;
}
The .h file is called "mathsfunctions.h" and has the following code in it:
int multiply (int, int);
In the main file, I add in the following include preprocessor directive:
#include "mathsfunctions.h"
//The rest of the code
After the above was coded, I imported the library. To do this, I did the following:
Toolbar -> Sketch -> Add Library -> c:.....\mathsfunctions
I can confirm that this is indeed imported because after doing such action, the same mathsfunctions folder appears in the Arduino libraries folder:
C:.....\Arduino\libraries\mathsfunctions
Problem: Upon compiling, the error dialogue box gives the following error:
mathsfunctions.h: No such file or directory
Assistance Required: Any idea on what the problem could be?
You should only have put the header and the source in the same directory as your main file. Also I would suggest putting the implementation in the header since this is the way that people generally include extra functions in C. I am unsure if C supports extra source files but it does support extra headers.
I'm stuck with a problem.
Well, I want to create my own dylib file, and inside of it write some methods, etc. You know, just a normal library.
Ok, so I opened Xcode, New project, and I selected Dynamic library. I've created a *main_lib.c* file, and wrote this method inside of it:
void printMessage(char *str){
printf("%s", str);
}
Obviously, I linked this file with the .dylib file and builded the project, no errors. After this, I created another project, selected Command Line Application and created a main.c file.
I imported the builded .dylib file in this project.
I asked one friend of mine, he said that at this point I should create an header file and write inside of it the function prototype of the dylib. So I created a main_lib.h file and declared this prototype:
void printMessage(char *str);
At this point, I write in the main.c file this code:
#include <stdio.h>
#include "main_lib.h"
int main(void){
printMessage("just a try");
return 0;
}
But when I build this it says:
ld: symbol(s) not found for architecture x86_64
It means that the compiler cannot find printMessage, right? But why? What am I doing wrong? Please help :) Thanks in advance guys!
To include it just add it to Target Dependencies and Link With Libraries in "Build Phases" and thats it, it should work.