开发者

Difference between gcc and g++ when running c++ program with boost library?

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 wit开发者_StackOverflow中文版h 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜