I'm using Eclipse 4.2, with CDT, and MinGW toolchain on a Windows machine (although I've a feeling the problem has nothing to do with this specific configuration). The G++ compiler is 4.7
I'm playing with c++11 features, with the following code:
#include <iostream>
#include <iomanip>
#include <memory>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
int main( int argc, char* argv[] )
{
vector<int> v { 1, 2, 3, 4, 5, 6, 7 };
int x {5};
auto mark = remove_if( v.begin(), v.end(), [x](int n) { return n<x; } );
v.erase( mark, v.end() );
for( int x : v ) { cout << x << ", "; }
cout << endl;
}
Everything is very straight forward and idiomatic c++11. The code compiles with no problems on the command line (g++ -std=c++11 hello.cpp).
In order to make this code compile In eclipse, I set the compiler to support C++11:
Properties -> C/C++ Build -> Settings -> Miscellaneous -> Ohter Flags:
I'm adding -std=c++11
Properties -> C/C++Build -> Discovery Options -> Compiler invocation arguments:
Adding -std=c++11
That's the only change I did to either the global preferences or to the project properties.
First Question: Why do I've to change the flags in two places? When each compiler flags is used?
If I hit Ctrl-B, the project will build successfully, as expected, and running it from within eclipse show the expected result (It prints: '5, 6, 7,').
However, the editor view shows red marks of error on both the 'remove_if' line, and the 'v.erase' line. Similarly, the Problems view shows I've these two problems. Looking at the details of the problem, I get:
For the remove_if line: 'Invalid arguments. Candidates are: #0 remove_if(#0, #0, #1)
For the erase line: 'Invalid arguments Candidates are: '? erase(?), ? erase(?,?)'
Second questions: It appears there are two different builds: one for continues status, and one for the actual build. Is that right? If so, do they have different rule (compilation flags, include paths, etc.)?
Third question: In the problem details I also see: 'Name resolution problem found by the indexer'. I guess this is why the error message are so cryptic. Are those messages coming from MinGW g++ compiler or from Eclipse? What is this Name resolution? How do I fix?
Appreciate your help.
EDIT (in reply to #Eugene): Thank you Eugene. I've opened a bug on Eclipse. I think that C++11 is only partially to blame. I've cleaned my code from C++11 stuff, and removed the -std=c++11 flag from both compilation switch. And yet, the CodAn barks on the remove_if line:
int pred( int n ) { return n < 5; }
int main( int argc, char* argv[] )
{
vector<int> v;
for( int i=0; i<=7; ++i ) {
v.push_back( i );
}
vector<int>::iterator mark = remove_if( v.begin(), v.end(), pred );
v.erase( mark, v.end() );
for( vector<int>::iterator i = v.begin(); i != v.end(); ++i ) {
cout << *i << ", ";
}
cout << endl;
}
The code compiles just fine (with Ctrl-B), but CodAn doesn't like the remove_if line, saying: Invalid Arguments, Candidates are '#0 remove_if(#0,#0,#1)'.
This is a very cryptic message - it appears it misses to substitute arguments in format string (#0 for 'iterator' and #1 for 'predicate'). I'm going to update the bug.
Interestingly, using 'list' instead of 'vector' clears up the error.
However, as for my question, I'm curious about how the CodAn work. Does it uses g++ (with a customized set of flags), or another external tool (lint?), or does it do it internally in Java? If there is a tool, how can I get its command line argument, and its output?
Build/Settings - these flags will be included into your makefile to do actual build. Build/Discovery - these flags will be passed to a compiler when "scanner settings" are discovered by IDE. IDE will run compiler in a special mode to discover values of the predefined macros, include paths, etc.
I believe, the problems you are seeing are detected by "Codan". Codan is a static analysis built into the CDT editor, you may find its settings on "C/C++ General"/"Code Analysis". You should report the problem to the bugs.eclipse.org if you feel the errors shown are bogus. Note that CDT does not yet support all C++11 features.
Related
Environment: Using Visual Studio Code with the Standard Microsoft C++ Debugging Extension. Using gdb as the underlying debugger.
For example: on the code below a step in on the commented line will jump to the standard string header.
Is there a way to avoid this?
#include <string>
#include <iostream>
int main()
{
std::string a = "Example String"; /// < Step IN here
std::cout << a << std::endl;
}
A more realistic example of why this is a problem:
In the case below I want to debug the a->methodToDebug()
but step in will send you to the source for the std::unique_ptr get method. You can still keep stepping in to get to your own code, but in more complicated code it becomes a pain.
#include <memory>
class A
{
public:
int methodToDebug() {
return -1 ;
}
};
int main()
{
auto a = std::make_unique<A>();
auto s = a->methodToDebug(); // <<-- Step in here, goes to get().
}
Turn on: Tools > Options > Debugging > General > [ ] Enable Just My Code.
It appears I can do this for recent versions of gdb(7.12.1 or higher) with the following in the .gdbinit file
skip -gfi /usr/include/c++/*/*/*
skip -gfi /usr/include/c++/*/*
skip -gfi /usr/include/c++/*
Well i just wanted to do a "foreach" like statement in C++, but in reverse order. Reading a little, the doc says that adding this to the "especial" form of the for statment it can be done.
Like this:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vecInt;
for (int i = 0; i < 10; i++)
vecInt.push_back(i);
for (auto const& x : vecInt | std::views::reverse) //Here is the problem with g++ -std=c++2a
{
}
return 0;
}
The thing is that im using ubuntu linux with the latest g++ compiler.
According to what the compiler throws me. I put an "std=c++20", but it then told me: "Did you mean std=c++2a" so, i don't know what the "a" means, but i tought it would be the C++20 standard. And finally discovered that "std::views::reverse" isn't seen by the compiler.
To go to the point: i want to do a reverse "foreach" C++ statement and i can't.
Any help?
I'm getting an error of invalid arguments in the Eclipse IDE for C/C++ Developers Photon (4.8.0) at map_name.insert(make_pair("string_name", int_name);.
I am using GCC 8.2.0. I'm trying some simple stuff with STL.
Tried either both insert(make_pair()) or insert(pair<string, int>()) getting same IDE error(Semantic error). Why is that?
Code:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> ages;
ages["Mike"] = 21;
ages["Johnny"] = 20;
ages["Vicky"] = 30;
ages["Mike"] = 42;
// ages.insert(make_pair("Peter", 100));
ages.insert(pair < string, int > ("Peter", 100));
for(map<string, int>::iterator it = ages.begin(); it!=ages.end(); it++)
{
cout<< it->first<<": "<< it->second<<endl;
}
return (0);
}
This is the error that is displayed in the IDE:
The standard library implementation that ships with GCC 8 uses a type trait intrinsic called __is_constructible, which Eclipse CDT's parser does not yet support.
This can result in false positive errors when CDT is made to parse GCC 8's standard library code.
If you use GCC 7 or earlier, you don't get any errors for this code.
UPDATE: This eclipse bug tracks adding support for __is_constructible to CDT's parser. It has recently been fixed, though the fix has not appeared in a CDT release yet.
I am facing a lot of saying Symbol 'array' could not be resolved, with code that is building fine.
#include <math.h>
#include <array>
#include <sstream>
#include <string>
static std::string printArray(std::array<double, 3> data) {
std::ostringstream strs;
strs << "( " << data[0] << " " << data[1] << " " << data[2] << " )";
return strs.str();
}
static std::string printVector(std::vector<double> data) {
std::ostringstream strs;
strs << "( " ;
for (const auto & value : data )
strs << value << " ";
strs << " )";
return strs.str();
}
The c++11 feature are activated using the -std=c++11 flag under C/C++ General -> Preposcessor Include Path, Macros etc. -> Porvides -> CDT GCC Built-in Compiler Settings as described here or here.
My question is no duplicate, since it works for std::vector and other c++11 features are handled correctly.
The header (#include <array>) can be resolved by pressing F3.
I am using Eclipse the CDT Version: Neon.3 Release (4.6.3).
I had the exact same symptoms that the OP reported:
I had previously applied the " -std=c++11" fix to make modern C++ things like "unique_ptr" syntax work.
std::vector and other std template things are resolved without errors.
I have #include <array> and F3 successfully navigates to the array header file.
I tried the solution provided by schorsch312, and it worked except the steps presented are not exactly consistent with my Neon version of Eclipse (same version used by OP). Hence, I had to adjust the instructions slightly. Here is what worked for me:
Select Project -> Properties -> C/C++ General -> C/C++ Paths and Symbols -> Symbols
On that page under Languages, select GNU C++
Add...
Name: __cplusplus
Value: 201103L
Check: Add to all configurations
Apply / OK.
Select Project -> C/C+ Index -> Rebuild -- if that operation did not automatically trigger.
Now, eclipse can resolve references to array properly. Note that this solution assumes you have previously resolved the general issue of getting c++11 syntax working.
The problem is that the symbol __cplusplus does not have the value 201103L.
To change this you need to
Edit Project->Preferences->C/C++ General/C/C+ General->Paths and Symbols->GNU C++->Symbols add define __cplusplus and set value to 201103L setzen.
Add under Window->Preferences->C/C++/Build/Settings/Discovery/CDT GCC Build-in Compiler Settings das Argument "-std=c++11" der Command Line hinzufügen
Activate under Project->Preferences->C/C++ General/C/C++ General/Path and Symbols->Providers for all entries the checkbox Use global provider shared between projects
#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?