简体   繁体   English

调用c函数时出现分段错误

[英]Segmentation fault when calling c-function

I create a static library (name it myStaticLib) containing the c_mainwrapper. 我创建一个包含c_mainwrapper的静态库(命名为myStaticLib)。 If I create a test-application I can call the function MainControllerInitiate(); 如果创建测试应用程序,则可以调用函数MainControllerInitiate(); without any problem. 没有任何问题。

When I create a shared library using myStaticLib and the c_wrapper I can call connectCreate(); 当我使用myStaticLib和c_wrapper创建共享库时,可以调用connectCreate(); without any problem. 没有任何问题。 But when I try to call MainControllerInitiate(); 但是当我尝试调用MainControllerInitiate(); I get a Segmentation fault. 我遇到了细分错误。

Files: 文件:
c_mainwrapper.h 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 c_mainwrapper.cpp

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

c_wrapper.h 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 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

found the problem by myselfe: 我自己发现了问题:

nobody asked for project files (tag qt). 没有人要求提供项目文件(标签qt)。 I compiled the static library as debug AND release. 我将静态库编译为debug AND release。 and in the shared library project-file I wrote: 在共享库项目文件中,我写了:

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 结论:如果要单步执行库代码并检查生成文件,请链接库调试版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM