Debug dll file with the dll source code C++ - c++

I Use QtCreator IDE with MSVC2017 in MS-Windows with QMake build-system. For summarizing my problem with debugging, I'll give you an example:
I create a project named library with this files:
library.h
#ifndef A
#define A
#include <stdio.h>
#ifdef __cplusplus
extern "C"
#endif
__declspec(dllexport) void some_function(void);
#endif
library.c
#include "library.h"
void some_function(void)
{
printf("We are in the %s::%d\n", __FUNCTION__, __LINE__);
}
And i made a .dll and .lib from my library project. I used in another project and while i try to debugging i could see the .dll function source code:
main.cpp
#include "library.h"
int main (void)
{
some_function(); /* Put the break point right here,
* And i could see the source code
* while debugging.
*/
}
In the above example everything is right, What could be problematic to not let me see the my .dll source code while debugging ?

I just change the library file name extensions from .c to .cpp and open both the project at the same time in QtCreator.

Related

Symbol(s) not found for architecture x86_64 when using C/C++ library in Xcode project

I am working on a Xcode project that uses a C/C++ library which I have imported the source code to my project.
I have created the correspondent bridging header and I am using a test method I have recently created to see if the code works. When building the project I get the error
Symbol(s) not found for architecture x86_64
The complete output of the error shown in Xcode is this
My deployment target is iOS 11 so no device should use 32 bits but I get errors related to 32 bits architectures.
MyApp-Bridging-Header.h
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "main.h"
Header file
#ifndef main_h
#define main_h
#include <stdio.h>
int my_print();
#endif /* main.h */
As the compiler complains about not finding definition of "_my_print", you could
1) check if the linking libraries include definition of _my_print
2) if you're using XCODE UI to build, try checking if all sources are added in path and libraries are intact
To me it seems straighforward linking issue and mis-configuration
After trying a lot of things that many people claimed it solved similar problems to this I came up with the solution without knowing it.
My previous main.h file was this one
#ifndef main_h
#define main_h
#include <stdio.h>
int my_print();
#endif /* main.h */
Surrounding my function with extern "C" solved it and now I can interact with my C and C++ library using Swift. Now the main.h file looks like this
#ifndef main_h
#define main_h
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
int my_print();
#ifdef __cplusplus
}
#endif
#endif /* main.h */
I am not 100% sure how this works so if anyone has a reasonable explanation why this works I am more than happy to hear it.

Calling function from dll with QT

I created by the first time a dll to use with C++ (and then C#, I have plans to share this dll between a C++ and C# applications) using QT creator but when I try to use it I get SEGFAULT error.
Here's my files:
mydll.h
#ifndef MYDLL_H
#define MYDLL_H
#include "mydll_global.h"
class MYDLLSHARED_EXPORT MyDll
{
public:
MyDll();
int getAnswer();
};
MYDLLSHARED_EXPORT int getNumber();
#endif // MYDLL_H
mydll_global.h
#ifndef MYDLL_GLOBAL_H
#define MYDLL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYDLL_LIBRARY)
# define MYDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYDLL_GLOBAL_H
mydll.cpp
#include "mydll.h"
MyDll::MyDll()
{
}
int MyDll::getAnswer()
{
return 42;
}
int getNumber()
{
return 10;
}
So I build it and created mydll.dll: then I went to other C++ project wheere I want to use this dll and put in the .pro file:
LIBS += "C:\path\to\mydll.h"
and in the main.cpp
#include "mydll.h"
and when I use function from dll like this:
qDebug() << getNumber();
I get a SEGFAULT error.
I thought that the header to provide the compiler type information and the dll to compiler to provide the function body was all I needed but as I'm getting a SEGFAULT I'm acessing NULL or someone else memory or so (I can't see the value on debug).
What am I missing?
First off, to link the DLL you need the link .lib file for that DLL. That file has all the binary manifest for linking.
Second, project file LIBS clause specify the list of .lib files to link with. Some of them may represent dynamic libraries (.dll).
See the example: Linking to Shared Library in Qt
Even better article covering both creation of DLL with Qt and using DLL in your Qt project: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application
I had this same issue. Almost every example I looked at had a class as their example, but I wanted to export functions like getNumber(). It's in the qt5 docs as you probably know by now.
This line needs to be in your header file:
extern "C" MYDLLSHARED_EXPORT int getNumber();
and it is either the mydll_global.h or mydll.h depending on what you are using.
mydll_global.h would be what qtcreator creates and puts export definitions in if you create a new Shared Library project.
I was thrown off guard by the extern "C" part as I wasn't using C and wasn't as familiar with how compilers export symbols.
Trying to add a public static function to a class definition using static MYDLLSHARED_EXPORT int getNumber(); or simply using a namespace (function defined with or without static) will give a not found for architecture x86_64 error using clang on osx.

Compile dll in Qt with VS compiler to use it in Borland

I have a package of C functions and I need to create DLL library from it, that can be used in C++ program. I haven't done any library before, so I am total beginner in that. I am working in Qt Creator.
My first try was to create it according to the manual Creating shared libraries, so I added these two lines to my project file:
TEMPLATE = lib
DEFINES += MYLIB_LIBRARY
Then I created mylib.h file
#ifndef MYLIB_H
#define MYLIB_H
#include "mylib_global.h"
#include "functions1.h"
#include "functions2.h"
#include "functions3.h"
class MYLIBSHARED_EXPORT Mylib
{
public:
Mylib(){};
};
#endif // MYLIB_H
Finally I added mylib_global.h:
#ifndef MYLIB_GLOBAL_H
#define MYLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYLIB_LIBRARY)
# define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYLIB_GLOBAL_H
To make functions usable in C++ I used these lines for each function in library
#ifdef __cplusplus
extern "C"{
#endif
void foo();
#ifdef __cplusplus
}
#endif
When compiling with MSVC2012 everything seems ok and I got some .dll file. But then I sent library to someone, who wanted to use it in Borland C++. He told me that I have to compile it with some DEF file to tell to VS compiler right names and with __stdcall instead of __cdecl. But I have no idea how to do it in Qt. Any explanation and help would be really appreciated. Thanks
P.S. I looked at posts Using VS dll in old Borland and Import VS dll in C Builder, but they
did not help me to understand the problem.
If you want to export the foo function, you need to tell the linker somehow.
What you were suggested is to use a .def file, which is quite easy.
Just create a file like exports.def in your project directory, and write in it something like:
EXPORTS
foo
Then go to your library project settings -> Linker -> Input -> Module Definition File
and fill in your .def file name

Visual Studio: Creating C++ dll and use it in VB Code

Using Visual Studio 2013, I am currently trying to generate a .dll with C++ Code, which i want to include into a VB.NET project. To create the .dll i tried to follow these tutorials:
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855/DLL-Tutorial-For-Beginners.htm
http://msdn.microsoft.com/de-de/library/ms235636.aspx
For now my .dll Project contains only the following files:
External Dependencies (auto generated by VS2013)
Header Files: stdafx.h, targetver.h (auto generated)
dllmain.cpp with default entry point Method DllMain and stdafx.cpp (auto generated)
MyDLL.cpp and containing the implementation of my methods which should be exported (for now one void method without parameters)
Headerfile MyDLL.h
Source.def (i only added the .def file after i tried using the __declspec(dllexport) statement)
MyDll.h contains:
#ifndef _DLL_MYDLL_H_
#define _DLL_MYDLL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR void MyMethod();
}
#endif
MyDLL.cpp contains:
#include "stdafx.h"
#include "MyDLL.h"
#define DLL_EXPORT
extern "C"
{
void MyMethod(){
//my code
}
When i create the .dll (by using "build > build solution" in VS2013) it compiles without errors and warnings. However, when i try to set a reference in my VB-Project by using "project > add reference" and selecting the .dll that was created in the DEBUG-Folder of my DLL-Project, i get an error stating that the reference could not be added and that i should make sure that the file is accessible and that it is a valid assembly or COM component.
Am i missing some vital settings in my dll/vb project here? Thanks in advance for your advice.

Create a DLL in C and link it from a C++ project

As per title, I'm trying to build a DLL using C and link it from a C++ project. I read and followed different tutorials on internet but everytime there is something missing and I don't understand what.
Here's what I did, step by step:
I created a new Win32 project, named testlib, then, from the wizard, I chose "DLL" and "Empty project".
Added a header:
//testlib.h
#include <stdio.h>
__declspec(dllexport) void hello();
Added a source; since I want it to be a C source I read I should simplly rename the .cpp file in .c, so
//testlib.c
#include "testlib.h"
void hello() {
printf("DLL hello() called\n");
}
Build succeded.
Now I would like to use my useful dll in another project.
Then: new project (testlibUse). This time I selected "Empty project".
No need to add an header, just created a cpp source
//main.cpp
#include <testlib.h>
int main() {
hello();
}
Then:
I added the path to the folder where is testlib.dll in Properties->VC++ directories->Executable directories
I added the path to the folder where is testlib.h in Properties->VC++ directories->Include directories
I added the path to testlib.lib (included extension) in Properties->Linker->Input->Additional dependencies
I tried to build but I got a linker error:
LINK : C:\path\testlibUse\Debug\testlibUse.exe not found or not built by the last incremental link; performing full link
main.obj : error LNK2019: unresolved external symbol "void __cdecl hello(void)" (?hello##YAXXZ) referenced in function _main
C:\path\testlibUse\Debug\testlibUse.exe : fatal error LNK1120: 1 unresolved externals
If I go back to testlib, rename back testlib.c in testlib.cpp and rebuild the dll, then I am able to build testlibUse but I get a "dll not found" error at runtime.
I tried also to change the configurations of both projects in "Release" (changing the path where needed), but nothing changed.
Sorry for the long post but I think it was necessary to write down exactly what I did.
Any suggestions?
Besides, are there any configuration parameters I need to change if I want to use my dll in a Qt project?
You have several problems:
The header file should mark the functions as exported when being compiled in the DLL but imported when being compiled by a library user.
The header file should wrap the function declarations in an extern "C" block when being compiled as C++ to ensure that the names do not get mangled
The DLL is not on your executable's library search path, so it can't be found at runtime.
To fix (1) and (2), rewrite your header like this:
#ifdef __cplusplus
extern "C" {
#endif
// Assume this symbol is only defined by your DLL project, so we can either
// export or import the symbols as appropriate
#if COMPILING_MY_TEST_DLL
#define TESTLIB_EXPORT __declspec(dllexport)
#else
#define TESTLIB_EXPORT __declspec(dllimport)
#endif
TESTLIB_EXPORT void hello();
// ... more function declarations, marked with TESTLIB_EXPORT
#ifdef __cplusplus
}
#endif
To fix (3), copy the DLL into the same folder as your executable file. The "executable directories" setting you're setting doesn't affect DLL searching -- see MSDN for a detailed description of how DLLs are searched for. The best solution for you is to copy your DLL into the directory where your executable file lives. You can either do this manually, or add a post-build step to your project that does this for you.
You should extern "C" the include:
extern "C" {
#include <testlib.h>
}
It looks as if you need to make sure the compiler not mangle the symbol name in the cpp build. You should be able to add an extern "C" to the definition in testlib.h:
#ifdef __cplusplus
extern "C"
#endif
__declspec(dllexport) void hello();
Here's a method for your C header file that could be included by C++. Make sure to set TESTLIB_EXPORTS only in your DLL preprocessor settings. In projects that include this header to use the DLL, the header will declare the functions as imports instead of exports.
The __cplusplus guard will tell the compiler to import your functions using C name decoration instead of C++ name decoration.
#include <stdio.h>
#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
TESTLIB_API void hello();
/* other prototypes here */
#ifdef __cplusplus
}
#endif