g++

why use g++ instead of gcc to compile *.cc files?

耗尽温柔 提交于 2019-12-21 05:04:32
问题 I compiled a library which use the g++ instead gcc. First I thought the source code was written in C++ but I found out later that there was not any C++ code in the *.cc files. To confirm this, I replaced the g++ in the original makefile with gcc. And I still got the correct program. Anyone can explain this? It was not the first time I met such a situation. 回答1: It depends on what exactly you changed in the makefile. gcc / g++ is really just a front-end driver program which invokes the actual

How to use eclipse to build C++ applications

时光毁灭记忆、已成空白 提交于 2019-12-21 04:42:23
问题 I have downloaded Eclipse Juno for C++ from here. I am trying to build a simple hello world program (mostly just to test out using eclipse for C++), but I can't get it to build and run. Here's my code: #include <iostream> using namespace std; int main() { cout << "hello" << endl; return 0; } The Problems tab shows make: *** No rule to make target `all'. I can only assume this is happening because eclipse is not configured to find my compiler ( g++ for archlinux), but I can't figure out how to

How do I get rid of LD_LIBRARY_PATH at run-time?

半世苍凉 提交于 2019-12-21 03:58:27
问题 I am building a C++ application that uses Intel's IPP library. This library is installed by default in /opt and requires you to set LD_LIBRARY_PATH both for compiling and for running your software (if you choose the shared library linking, which I did). I already modified my configure.ac / Makefile.am so that I do not need to set that variable when compiling, but I still can't find the shared library at run-time; how do I do that? I'm compiling with the -Wl, -R/path/to/libdir flag using g++

C++ constant folding a loop for prime numbers

青春壹個敷衍的年華 提交于 2019-12-21 03:55:19
问题 Having had a look at previous questions 1, 2 , I was wondering if I can force the compiler to perform a constant folding for the following code which prints prime numbers. #include <iostream> using namespace std; inline bool is_prime(int n) { if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } int main() { for(int i=0;i<20;i++) if(is_prime(i)) cout<<i<<endl; return 0; } And I build it via: g++ -O3 -S main.cpp -o main.asm As the result are a few: 2,3,5,7,11,13

Including C Code in C++

ぃ、小莉子 提交于 2019-12-21 03:45:16
问题 I'm trying to include C code into a simple C++ program but I ran into an unexpected problem - when I try to compile the program g++ gives the following error: /tmp/cccYLHsB.o: In function `main': test1.cpp:(.text+0x11): undefined reference to `add' I searched for a solution and found this tutorial: http://www.parashift.com/c++-faq/overview-mixing-langs.html There seems to be no difference to my program so I'm a bit lost... My C++ program looks like this: test1.ccp #include <iostream> using

Compile same file with different flags using CMAKE

匆匆过客 提交于 2019-12-21 03:19:14
问题 I want to compile the same .cpp source file into two different target executables and I am using cmake. One will have some instrumentation code and the other won't. That way I can compare the overhead of instrumentation. I have the instrumentation code separated with #ifdefs so I want to define a value using the -D flag. I see that is possible with add_definitions(-DINSTRUMENT) But it looks like this then applies to all the executables created in that directory. I'm wondering if there is a

How to check for inf (and | or) NaN in a double variable

馋奶兔 提交于 2019-12-21 03:18:08
问题 Consider the following code: #include <iostream> #include <cstdio> #include <cstring> using namespace std; template<class T> bool IsNaN(T t) { return t != t; } int main(int argc, char**argv) { double d1, d2; sscanf(argv[1], "%f", &d1); sscanf(argv[2], "%f", &d2); double dRes = d1/d2; cout << "dRes = " << dRes << "\n"; if(IsNaN(dRes)) cout << "Is NaN\n"; else cout << "Not NaN\n"; } Couple of questions: When I pass 0 and 0 as arguments, it outputs dRes = inf . But I was expecting dRes = NaN or

Is it possible to use anonymous classes in C++?

五迷三道 提交于 2019-12-21 03:14:10
问题 I have seen anonymous classes in C++ code on Quora. It's successfully compiled and run. Code here: #include <iostream> auto func() { class // no name { public: int val; } a; a.val = 5; return a; } int main() { std::cout << func().val << std::endl; return 0; } So, Is it valid in C++? Also, I am curious to know, Is it possible to use anonymous classes in C++? 回答1: In C++, an anonymous union is a union of this form: union { ... } ; It defines an unnamed object of an unnamed type. Its members are

Rule for lambda capture variable

醉酒当歌 提交于 2019-12-21 02:21:36
问题 For example: class Example { public: explicit Example(int n) : num(n) {} void addAndPrint(vector<int>& v) const { for_each(v.begin(), v.end(), [num](int n) { cout << num + n << " "; }); } private: int num; }; int main() { vector<int> v = { 0, 1, 2, 3, 4 }; Example ex(1); ex.addAndPrint(v); return 0; } When you compile and run this in MSVC2010 you get the following error: error C3480: 'Example::num': a lambda capture variable must be from an enclosing function scope However, with g++ 4.6.2

Rule for lambda capture variable

落爺英雄遲暮 提交于 2019-12-21 02:20:35
问题 For example: class Example { public: explicit Example(int n) : num(n) {} void addAndPrint(vector<int>& v) const { for_each(v.begin(), v.end(), [num](int n) { cout << num + n << " "; }); } private: int num; }; int main() { vector<int> v = { 0, 1, 2, 3, 4 }; Example ex(1); ex.addAndPrint(v); return 0; } When you compile and run this in MSVC2010 you get the following error: error C3480: 'Example::num': a lambda capture variable must be from an enclosing function scope However, with g++ 4.6.2