调用c-function时出现分段错误

时间:2021-05-17 03:10:28

I create a static library (name it myStaticLib) containing the c_mainwrapper. If I create a test-application I can call the function MainControllerInitiate(); without any problem.

我创建了一个包含c_mainwrapper的静态库(将其命名为myStaticLib)。如果我创建一个测试应用程序,我可以调用函数MainControllerInitiate();没有任何问题。

When I create a shared library using myStaticLib and the c_wrapper I can call connectCreate(); without any problem. But when I try to call MainControllerInitiate(); I get a Segmentation fault.

当我使用myStaticLib和c_wrapper创建共享库时,我可以调用connectCreate();没有任何问题。但是当我尝试调用MainControllerInitiate()时;我收到了分段错误。

Files:
c_mainwrapper.h

(...)
typedef void* MainController_p;
extern MainController_p (*createMainController)()

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC void MainControllerInitiate();
(...)

#undef EXTERNC
(...)

c_mainwrapper.cpp

(...)
MainController_p (*createMainController)() = NULL;
(...)
void MainControllerInitiate()
{
    if (createMainController != NULL)
    {
        createMainController();
    }
}
(...)

c_wrapper.h

(...)
#include <c_mainwrapper.h>

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC void connectCreate();
(...)

#undef EXTERNC
(...)

c_wrapper.cpp

(...)
MainController_p createController()
{
    return new MainController();
}

void connectCreate()
{
    createMainController = &createController;
}
(...)

Edit:
The failing test-application is just:

编辑:失败的测试应用程序只是:

#include <c_wrapper>

int main(int argc, char *argv[])
{
    connectCreate();
    MainControllerInitiate(); // here the Segmentation fault occures

    return 0;
}

Edit2: Added the missing createMainController = NULL

Edit2:添加了缺少的createMainController = NULL

1 个解决方案

#1


3  

found the problem by myselfe:

myselfe发现了这个问题:

nobody asked for project files (tag qt). I compiled the static library as debug AND release. and in the shared library project-file I wrote:

没有人要求提供项目文件(标签qt)。我将静态库编译为debug AND release。在我写的共享库项目文件中:

CONFIG(release : debug|release)
{
    unix:!macx: LIBS += -L../../Build_Directory/x86/Release/myStaticLib \
    -lmyStaticLib
}
CONFIG(debug: debug|release)
{
    unix:!macx: LIBS += -L../../Build_Directory/x86/Debug/myStaticLib \
    -lmyStaticLib
}

The problem is, i used the wrong syntax. I has to be

问题是,我使用了错误的语法。我必须是

CONFIG(release, debug|release){

and not

CONFIG(release : debug|release)
{

so I linked the shared library against the release version of the static library everytime. The error in the shared library was forced by myself. But i thought i would use the debug-version of the static library and could step through the code til the error.

所以我每次都将共享库与静态库的发布版本链接起来。共享库中的错误是我自己强制的。但我想我会使用静态库的调试版本,并可以逐步完成代码直到错误。

conclusion: link against library debug-version if you want to step through the library code and check the make-files

结论:如果要逐步执行库代码并检查make-files,请链接库调试版

#1


3  

found the problem by myselfe:

myselfe发现了这个问题:

nobody asked for project files (tag qt). I compiled the static library as debug AND release. and in the shared library project-file I wrote:

没有人要求提供项目文件(标签qt)。我将静态库编译为debug AND release。在我写的共享库项目文件中:

CONFIG(release : debug|release)
{
    unix:!macx: LIBS += -L../../Build_Directory/x86/Release/myStaticLib \
    -lmyStaticLib
}
CONFIG(debug: debug|release)
{
    unix:!macx: LIBS += -L../../Build_Directory/x86/Debug/myStaticLib \
    -lmyStaticLib
}

The problem is, i used the wrong syntax. I has to be

问题是,我使用了错误的语法。我必须是

CONFIG(release, debug|release){

and not

CONFIG(release : debug|release)
{

so I linked the shared library against the release version of the static library everytime. The error in the shared library was forced by myself. But i thought i would use the debug-version of the static library and could step through the code til the error.

所以我每次都将共享库与静态库的发布版本链接起来。共享库中的错误是我自己强制的。但我想我会使用静态库的调试版本,并可以逐步完成代码直到错误。

conclusion: link against library debug-version if you want to step through the library code and check the make-files

结论:如果要逐步执行库代码并检查make-files,请链接库调试版