I get undefined reference errors to various things in the boost:: namespace if I attempt to compile programs that use any parts of Boost Libraries in Qt Creator 2.5.2. At first I thought it's because I was mixing static Boost libraries with shared Qt libraries, so I recompiled Boost with link=shared runtime-link=shared build option, but the problem remains. I then started a Non-Qt, Plain C++ project that consists of nothing more than a main.cpp containing a slightly modified Boost test program:
// main.cpp, cin-less version of
// http://www.boost.org/doc/libs/1_51_0/more/getting_started/windows.html#test-your-program
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main() {
std::string headerLines = "To: George Shmidlap\n" \
"From: Rita Marlowe\n" \
"Subject: Will Success Spoil Rock Hunter?\n" \
"---\n" \
"See subject.\n";
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
boost::smatch matches;
std::string::iterator newLinePos = std::find(headerLines.begin(), headerLines.end(), '\n');
std::string::iterator startPos = headerLines.begin();
while(newLinePos != headerLines.end()) {
if (boost::regex_match(std::string(startPos, newLinePos++), matches, pat)) {
std::cout << "\nRegex Match: " << matches[2];
}
startPos = newLinePos;
newLinePos = std::find(startPos, headerLines.end(), '\n');
}
char temp[3];
std::cin.getline(temp, 2);
return 0;
}
Project file:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp
Debug {
LIBS += -lboost_regex-mgw46-mt-d-1_51
}
release {
LIBS += -lboost_regex-mgw46-mt-1_51
}
Compiling the above project from within Qt Creator, or using mingw32-make in command line, gives:
E:\BoostTest-483-MinGW_Debug\debug\main.o:-1: In function `ZN5boost13match_resultsIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEE17raise_logic_errorEv':
c:\tdm-mingw32\include\boost\regex\v4\match_results.hpp:562: error: undefined reference to `boost::throw_exception(std::exception const&)'
E:\BoostTest-483-MinGW_Debug\debug\main.o:-1: In function `ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsE':
c:\tdm-mingw32\include\boost\regex\v4\perl_matcher_common.hpp:55: error: undefined reference to `boost::throw_exception(std::exception const&)'
[etc...]
Compiling main.cpp from command line, without Qt Creator or mingw32-make, works just fine:
E:\BoostTest>g++ -s -O3 main.cpp -o main.exe -lboost_regex-mgw46-mt-1_51
E:\BoostTest>main.exe
Regex Match: Will Success Spoil Rock Hunter?
E:\BoostTest>g++ -s -O3 main.cpp -o main-dbg.exe -lboost_regex-mgw46-mt-d-1_51
E:\BoostTest>main-dbg.exe
Regex Match: Will Success Spoil Rock Hunter?
Tested with:
TDM-MinGW32 (MinGW 4.6.1)
My own builds of MinGW:
MinGW 4.6.3 with dwarf2 exceptions handling
MinGW 4.6.3 with SJLJ exceptions handling
Boost Libraries 1.51.0 (built from source with each of the above compiler builds, static and shared libraries)
Qt Framework 4.8.3 for MinGW, precompiled binaries.
Qt Creator 2.5.2 for Windows.
I've checked Qmake's makescpecs configuration files, among other things, but still cannot figure out the root of the problem. Any ideas?
This is most likely far too late to help you, but I've just had to figure this one out today. I too am using mingw32 and Qt and had the same undefined reference.
I first resolved it by adding the following in `some file', based on another StackOverflow question:
namespace boost
{
void throw_exception(std::exception const &e) { assert(false); }
}
However I actually wanted to throw an exception, so changed the assert(false); to a throw e;. That immediately threw up the compile error:
error: exception handling disabled, use -fexceptions to enable
...which gives a clue.
The trick turned out to be adding CONFIG += exceptions (along with console so cout/printf etc. actually do anything, that was a real pain!) to my qmake file. I have no idea what is really going on here, maybe most Linux distros add something special to the qmake spec files or whatever.
Related
I have installed the latest version of both sphinxbase and pocketsphinx on my mac. In this CMU's site
they have provided a simple hello world code to test the validity of the installation. The code looks like:
#include <pocketsphinx.h>
int main(int argc, char *argv[])
{
ps_decoder_t *ps = NULL;
cmd_ln_t *config = NULL;
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "/en-us/en-us",
"-lm", MODELDIR "/en-us/en-us.lm.bin",
"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
NULL);
return 0;
}
and I have to enter this command below to compile using GCC
gcc -o hello_ps hello_ps.c \
-DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\" \
`pkg-config --cflags --libs pocketsphinx sphinxbase`
where pkg-config would take care about include and library paths.
But the compilation returns the following errors in the stdio.h file (pocketsphinx.h includes the stdio.h).
Restrict requires a pointer or reference (int is invalid)
in several places and also
Unknown typename _FILE (Expanded from macro 'FILE')
I went through stdio.h file and found these corresponding statements and I have no clue what it does. I understand that restrict keyword is not a part of C++ standard but even when compiling with c99, it still throws the same error. Any help would be highly appreciated.
According to the log you have an extra file /usr/local/include/stdio.h which is not a standard include file. This file was installed by some other software and causes problems.
You need to remove problematic stdio.h (and probably other extra headers) from your system.
Correct stdio.h is in /usr/include folder.
I'm trying to use the C++ compiler to compile the following program:
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
llvm::errs() << "Expected an argument - IR file name\n";
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::SMDiagnostic err;
llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );
if( !mod )
{
err.print( argv[0], errs() );
return 1;
}
return 0;
}
I'm trying to compile the program using the following command:
clang++ main.cpp -o main
However, when I compile, I'm getting the following compile error:
main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
^
1 error generated.
In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.
Any help would be greatly appreciated.
You can use the following command:
g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main
Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.
Thank You
You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5
You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout
Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.
I have compiled TBB from source using Mingw following the comment #5 in this post: http://software.intel.com/en-us/forums/topic/291331. That went ok.
When I try to use the new TBB library in a QtCreator project, I end with this errors (ignore the warning messages): http://postimage.org/image/yrrecugix/
Here's the sample code I tried (I omit the non-tbb code):
#include "tbb/task_scheduler_init.h"
int main()
{
tbb::task_scheduler_init init;
/// more things.
}
And here's the .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp
#QMAKE_CXXFLAGS += -fopenmp
#QMAKE_LFLAGS += -fopenmp
INCLUDEPATH += "E:\TRABAJO\LIBRERIAS\tbb-4.1_src\include"
LIBS += -L"E:\TRABAJO\LIBRERIAS\tbb-4.1_src\build\windows_intel64_gcc_mingw4.5.4_debug\" \
-ltbb_debug
Any idea?.
Thanks!.
When built with MinGW on Windows, TBB binaries are tbb.dll and tbb_debug.dll. The option -ltbb_debug in your configuration files probably causes the linker to look for libtbb_debug.<something>. It can't find such a binary and reports about unresolved symbols.
I wrote a c++ program using boost library in Xcode. Here is my code. It is very simple.
#include <iostream>
#include </usr/local/include/boost/math/special_functions/beta.hpp>
using namespace std;
using namespace boost::math;
int main (int argc, char * const argv[])
{
double a = 100.0;
double b = 100000.0;
double x = 0.3;
double result = beta(a, b, x);
cout << result << endl;
return 0;
}
But when I tried to build it in the Xcode, there popped up a lot of errors related to the library linking stuff. I noticed that the compiler that Xcode was using was "System Default: gcc 4.2". And all other options are gcc or LLVM gcc (I have no idea what this is).
I later tried to compile the file simply using terminal. Weird thing happened. If I compile it with g++, without any extra flags, the compilation completed successfully and the the program could be ran normally; but if I compile it with gcc, there are pages of errors.
So, to sum it up, while using g++, everything is OK; while using gcc, everything is not OK. Since the Xcode is using gcc, the program could not be compiled using Xcode.
(And I kind of need to use the Xcode because this is just a test program, I actually have a much bigger project to handle and I depend on the debugger of Xcode.)
So my question is, WHAT THE HELL is the difference between gcc and g++? Or how can I change the compiler of Xcode to g++?
gcc is a C compiler.
g++ is a C++ compiler.
You're trying to compile C++, ergo, you need to use a c++ compiler.
Googling "Using XCode for c++" brings up lots of results, but this one seemed fairly straightforward and had pictures:
https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html
The gcc command compiles C files (although you can use -libstdc++) to link C++ files as well but I don't recommend it.
The g++ command works for C++ files which is why it worked in your case.
For XCode you have to change the compiler from GCC to G++ for it to successfully work.
I have a mixture of c and c++ files compiling under g++. As explained in:
What is the difference between g++ and gcc?
The c files are being compiled as c++ with the g++ command line. Not huge problem but migrating over to gcc will allow th c files to compile as c files and the c++ file to compile as c++.
What -I includes or -L libraries do I need to add to the gcc command line, that the g++ command line is including by default?
You shouldn't need to add any includes or libraries beyond what you already have.
Whatch out for C functions being called from C++ code - you need to tell the C++ compiler those are C functions so the program is linked correctly and works.
The standard practice is to add the following directives to all your C headers being included in C++ files:
#ifdef __cplusplus
extern "C" {
#endif
... C header contents go here ...
#ifdef __cplusplus
}
#endif
More info here: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
You shouldn't need to add any. If it's using C++ it should automatically bring in C++ libraries.
If not, you'll want -lstdc++ (and if you're still getting undefined references, -lc for the libc). Don't forget -lm if you use math functions.
GCC can determine which language a file is in based on the file extension. However, GCC does not automatically link in run time support for any language other than C. In practice that means you can compile C++ programs using gcc instead of g++ but you'll need to add the -lstdc++ directive:
#include <iostream>
int main()
{
std::cout << "Hello world\n";
}
g++ hello.cc
gcc hello.cc -lstdc++
More accurately, you will need to specify -lstdc++ if you you use the standard library, exceptions, operator new, or RTTI. For instance, try compiling the following without -lstdc++:
int main()
{
try {
throw 1;
}
catch (int i)
{
return i;
}
}
Please note that STL containers (including std::strings) use operator new by default. Strictly speaking you should be able to use the algorithms (std::min, std::find_first_of, etc.) binders and a few other things in the standard library without -lstdc++ but for the most part you might as well include it (the linker will ignore any libraries that you don't actually link to).
Why not compile the c objects with gcc and the c++ with g++ and then when you link, link using the g++?