简体   繁体   English

如果在Qt Creator中进行编译,库函数的行为将有所不同

[英]Library function behaves differently if compiled in Qt Creator

I have a c program, that if I build it in the shell with this command: 我有一个c程序,如果我使用以下命令在shell中构建它:

gcc -o simpledemo -fpic -fsigned-char -DPLATFORM_LINUX -Iinclude/ simpledemo.c ../AcapelaLibs/libbabile.a -lstdc++

it compiles, runs, and produces the expected output. 它编译,运行并产生预期的输出。

gcc info: gcc信息:

/usr/bin/gcc
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

However, if I compile it with Qt Creator (Qt 5.5.1), it compiles and runs but a NULL value is returned from a function of the library I use. 但是,如果我使用Qt Creator(Qt 5.5.1)进行编译,它将编译并运行,但是我使用的库的函数会返回NULL值。

Qt uses the following g++ : Qt使用以下g++

/usr/bin/g++
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

I think it might be something with the compiler flags. 我认为可能与编译器标志有关。 This is what I'm doing in Qt to match the shell command: 这是我在Qt中所做的,以匹配shell命令:

in .pro file: .pro文件中:

QMAKE_CXXFLAGS  += -DPLATFORM_LINUX
INCLUDEPATH     += $$PWD/include/

I think the INCLUDEPATH is OK because I can access files there from within my code (they are recognizable). 我认为INCLUDEPATH是可以的,因为我可以从代码中访问文件(它们是可识别的)。

And note that in the shell compilation I use a library ../AcapelaLibs/libbabile.a , so in Qt I click Add Library... and choose that same library, and Qt adds it to the .pro file. 并注意在shell编译中,我使用一个库../AcapelaLibs/libbabile.a ,因此在Qt中,我单击Add Library ...并选择相同的库,然后Qt将其添加到.pro文件中。 Also here I can access library functions from within my code. 同样在这里,我可以从代码中访问库函数。

The problem is that a function from libbabile.a returns NULL if I compile and run from Qt (or compile in Qt and run from shell). 问题是,如果我从Qt编译并运行(或在Qt中编译并从shell运行),那么来自libbabile.a的函数将返回NULL。

What's the difference between the 2 methods that makes one of them succeed and the other fail? 使两种方法中的一种成功而另一种失败的方法有什么区别?

Library function behaves differently if compiled in Qt Creator. 如果在Qt Creator中进行编译,则库函数的行为会有所不同。 I think it might be something with the compiler flags. 我认为可能与编译器标志有关。

What compiler flags are different from used in command line? 哪些编译器标志与命令行中使用的标志不同?

You can examine Qt Creator compiler output and get all the compiler options specified from there. 您可以检查Qt Creator 编译器输出,并从那里获取所有指定的编译器选项。 Then you can compare those with expected set of options and deliberately add or remove those options in your project .pro file: 然后,您可以将这些选项与期望的选项集进行比较,并有意在项目.pro文件中添加或删除这些选项:

# C++ flags
QMAKE_CXXFLAGS += -opt1 -opt2 # add
QMAKE_CXXFLAGS -= -opt1 -opt2 # remove

QMAKE_CXXFLAGS_RELEASE += -opt1 -opt2 # add
QMAKE_CXXFLAGS_RELEASE -= -opt1 -opt2 # remove

QMAKE_CXXFLAGS_DEBUG += -opt1 -opt2 # add
QMAKE_CXXFLAGS_DEBUG -= -opt1 -opt2 # remove

# C flags, slightly different macro
QMAKE_CFLAGS += -opt1 -opt2 # mind add/remove/debug/release

For both debug and release modes and separately. 分别用于调试和发布模式。

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

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