1

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
ScottK
  • 1,526
  • 1
  • 16
  • 23
  • when you compile within eclipse, depending on your project settings it creates it's own "Makefile" ... what you have to do is set your project settings to use your own Makefile. Uncheck "build Makefiles Automatically" from within C/C++ Build options – avrono Mar 06 '17 at 16:20
  • This sounds like a good work-around, but I have set the -std=c++11 flag, so it should find this function without me creating a makefile. I was hoping someone has seen this before and could let me know if installing Eclipse with just the Java target at first, may have affected my C++ environment which I added later. – ScottK Mar 06 '17 at 18:18
  • 1
    it should not make a difference. More likely that it's not picking up CC Flags due to configuration – avrono Mar 08 '17 at 08:45
  • Thanks @avrono, I will try creating my own makefile and see if that gets around the problem. – ScottK Mar 08 '17 at 08:46
  • @avrono: Thanks for the suggestion but it turns out that the makefile made no difference. What I failed to realize is that Eclipse C provides the compiler, but not the headers. I think I found them in the Eclipse CDT package. – ScottK May 28 '17 at 15:33

1 Answers1

0

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 chooseISO C++11 (-std=c++0x)underLanguage Standard`.

The project then built successfully and ran correctly. Thanks for everyone on StackEchange who helped with your various posts.

ScottK
  • 1,526
  • 1
  • 16
  • 23