问题
Elipse cannot build C++ programs on my system if they contains calls to string's function stod(). I am running Ubuntu 14.04 with Eclipse using the C/C++ perspective.
I have included the header, and compiled with the -std=c++11 compile flag and set the compiler from gcc to g++, but it did not work. I also tried using Eclipse's "ISO C++11" dialect setting (which sets the compile flag to -std=c++0x. In all cases, the compiler is unable to see the stod function.
To isolate the problem, I compiled the example from the cplusplus.com website for stod:
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
This fails to compile, with the error "Function stod could not be resolved". If I remove those calls, and initialize earth to 0.0, and moon to 1.0, it compiles and runs fine (with the wrong answer of course).
I tried compiling the same program from the Ubuntu command line and it works when I include the -std=c++11 compile flag.
> g++ -o teststod teststod.cpp -std=c++11
> teststod
The moon completes 12.3684 orbits per Earth year.
My Linux is up to date, and I have done an apt-get update to be sure. One possible clue, I originally installed Eclipse with only the Java target, but I created these projects in Eclipse as C++ projects with C/C++ perspectives.
For reference, here is what Eclipse's About page returns:
IDE for Java Developers
Version: Mars.1 Release (4.5.1)
Build id: 20150924-1200
Ubuntu version:
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
回答1:
After taking a step back, I decided to reinstall everything. After reviewing a youTube video on setting up Eclipse C on windows, I realized that installing Eclipse to support C required these steps:
- Downloading Java JDK from java.oracle.com (done)
- Downloading Eclipse (done)
- Downloading a C/C++ compile environment (not done!)
- Downloading Eclipse CDT package (not done)
Like many people, I thought I could simply download Eclipse for C/C++ and have it work out of the box, but I could not build any of the tutorials like helloWorld. I needed to download the compiler toolchain separately.
I downloaded and installed cygwin and the Eclipse CDT package, but I still got errors because Eclipse could not find the standard include file headers, nor the make program.
This post talks about setting the PATH
. I had to set the project build environment PATH
variable under Window->Preferences->C/C++->Build->Environment
and added the path C:\cygwin64\bin
to the PATH
variable.
Under Project Properties, I had to go to the Tool Chain Editor
located under C/C++ Build
and set Current Toolchain
to CygWin GCC
and Current builder
to CDT Internal Builder
according to this post.
And because I was starting from scratch, I had to go back and set -std=c++0x
for the project under <project>
Properties->C/C++ Build->Settings->Dialect' and choose
ISO C++11 (-std=c++0x)under
Language Standard`.
The project then built successfully and ran correctly. Thanks for everyone on StackEchange who helped with your various posts.
来源:https://stackoverflow.com/questions/42629820/eclipse-c-on-ubuntu-14-04-cannot-find-strings-stod