eclipse 4.2 cdt with ubuntu 12.04 configuration - c++

I am running Eclipse CDT (Eclipse v. 4.2) on Ubuntu 12.04. When I create a C++ project (Under Project type -> Executable -> Hello World C++ Project) and select the Linux GCC toolchain, the standard hello world application compiles and runs just fine. When I try to create an Empty Project (from the same menu as before), selecting the Linux GCC toolchain also, it brings several build errors:
make:***[tst] Error 1
Symbol 'cout' could not be resolved
Symbol 'endl' could not be resolved
Symbol 'std' could not be resolved
undefined reference to 'main'
When I compare the path settings (Properties -> C/C++ Build, and C/C++ General) for the .cpp file generated by "Hello World C++ Project", and for the .cpp file added to the "Empty Project" they have the same configurations and settings, so I am not sure why the empty project isn't detecting the standard library and compiling. All the files in the includes folder are also the same. This also happens on my Windows 7 (MingW compiler) install, and on my MacOS X install. For now I can just create my projects as a "Hello World C++ project", get rid of the text and code what I want, but I am really curious as to how to set it up from scratch, especially since it is my understanding that creating an "Empty Project" under the Executable folder (as opposed to creating a Makefile project) should include a makefile and all the necessary paths (as long as the proper toolchain is selected?).
The actual build error from the CDT console is:
20:49:26 **** Incremental Build of configuration Debug for project tst ****
make all
Building target: tst
Invoking: GCC C++ Linker
g++ -o "tst" ./src/test.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [tst] Error 1
20:49:26 Build Finished (took 62ms)
The code I'm trying to run is just the default hello world code generated by eclipse:
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Thanks in advance.

I've seen that some of the Ubuntu 12.04 DVD's sold in Magazines do not install gnu G++ by default. Go to the Ubuntu Software Center and install G++ (GNU C++ compiler tool chain). After installation right click on your project's name in the Eclipse Project Explorer on the left side of the IDE. Select Rebuild under the Index item. This should resolve the issue with the iostream include, etc.

Related

Setting up ROOT from Cern in Xcode, linking the librariers correctly

I want to set up ROOT from CERN in my Xcode IDE but I'm having problems linking the libraries. I'm using root 6.04.14 and xcode 7.3.
I created a mock up project where I simply have a .cpp where I include a basic class from root (#include "TFile.h"). This I can compile from command line by:
clang++ -std=c++11 -I/opt/root/root-6.04.14/include/root -L/opt/root/root-6.04.14/lib/root -lCore main.cpp
Now it comes to setting up everything in the Xcode IDE. I included "/opt/root/root-6.04.14/include/root" in the header search path and Xcode is not complaining, so I guess it finds the header files. I tried adding "/opt/root/root-6.04.14/lib/root -lCore" to the library search path but I get errors:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:301:
/opt/root/root-6.04.14/include/root/Math/math.h:65:11: error: no member named 'log1p' in the global namespace; did you mean simply 'log1p'?
return ::log1p(x);
^~
/opt/root/root-6.04.14/include/root/Math/math.h:63:15: note: 'log1p' declared here
inline double log1p( double x) {
^
/opt/root/root-6.04.14/include/root/Math/math.h:76:11: error: no member named 'expm1' in the global namespace; did you mean simply 'expm1'?
return ::expm1(x);
^~
/opt/root/root-6.04.14/include/root/Math/math.h:74:15: note: 'expm1' declared here
inline double expm1( double x) {
and so on...
Furthermore when I look at the terminal command Xcode is running(at least that is what I think it does) there is no "-L/opt/root/root-6.04.14/lib/root -lCore" included. I then tried to put "-L/opt/root/root-6.04.14/lib/root -lCore" into other linker flags. Now it is included in the terminal command but still giving me the same error.
Question1:
I noticed Xcode is running "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" while I have been using clang++, where is the difference and how can I change it?
Question2:
What is the difference between adding the directory to the library search path and putting it in via the linker flag?
Question3:
The big one, where do I mess up?
You've probably already figured this out, but just in case someone else stumbles on this issue here's how I've setup my ROOT (v6.06.04) in xcode. To demonstrate, lets start from scratch with a fresh xcode project.
Say we want to run the following program in xcode (note that it uses ROOT classes)
#include <iostream>
#include "TApplication.h"
#include "TCanvas.h"
#include "TGraph.h"
#include <vector>
int main(int argc, const char * argv[]) {
// Create a TApplication so that we can get plot the histogram
TApplication* myApp = new TApplication("myApp", 0, 0) ;
// Create some vector information
std::vector<double> x(100), y(100) ;
for (int i=0; i<x.size(); i++) {
x[i] = i * (10.0/x.size()) ;
y[i] = std::cos(x[i]) ;
}
// Create a TGraph
TGraph* graph = new TGraph(x.size(), &x[0], &y[0]) ;
// Create a canvas and draw the graph
TCanvas* canvas = new TCanvas("canvas","canvas") ;
graph->Draw("ap0") ;
canvas->Update() ;
// Run the TApplication to produce all of the plots
myApp->Run() ;
return 0;
}
If you just copy and paste this program into xcode you'll see that the ROOT headers arent recognized by xcode and you get the error 'XXX.h' file not found. This is obviously because we need to tell xcode where to find the headers.
Click on your project in the left-side menu
Under "Build Settings" click the "+" -> "Add User-Defined Settings".
This will add a parameter under the "User-Defined" section. Call the new parameter "ROOTSYS" and point it to the top directory of your ROOT installation. For me this is /Users/user/root_cern/root_v6.06.04/. (Note: This step isnt absolutely necessary but makes the rest less painful and allows you to update your ROOT installation without having to change the header and library paths below)
Now (still in "Build Settings") go to "Search Paths" -> "User Header Search Paths". In this field add the path "$(ROOTSYS)/include"
Set the "Search Paths" -> "Always Search User Paths" field to Yes
At this point the nasty errors on our headers have gone away! Unfortunately, the program wont build. A look at the build errors shows that there are TONS of linking errors! Clearly we need to update our linker flags to include all the ROOT libraries we want to compile against.
Open a terminal and run $ROOTSYS/bin/root-config --libs. For me the output is:
-L/Users/user/root_cern/root_v6.06.04/lib -lCore -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lpthread -Wl,-rpath,/Users/user/root_cern/root_v6.06.04/lib -stdlib=libc++ -lm -ldl
Copy the output from the above root-config and paste it under "Build Settings" -> "Linking" -> "Other Linker Flags". Note that you can replace all instances of the path to your ROOT install directory with $(ROOTSYS) and you wont have to update them in the future when you update your ROOT version.
Now the program should build just fine! If you're STILL getting the linking errors, make sure that the TARGET you're trying to build inherits it's linking information from the project. Do this by going to "Other Linker Flags" for the target you are building and set it to $(inherited). You'll get the handy plot of a cos(x) function.
A few notes:
If you dont use a TApplication, the program will end before you get a chance to look at your plot.
You can end the program either through the xcode GUI or by selecting "File" -> "Quit Root" in the plot's menu bar.
Things get a bit more complicated if you're trying to write a class that you want ROOT to be able to stream out to the file. It's possible to do it by adding in an additional "Run Script" build phase to your target. For now though, this should get you started.
Thank you for your detailed guide, I followed it as precise as possible. Only the remark: $(inherited) I am not sure. Unfortunately it does not work on my MAC (M1), Xcode 14.0. Installed ROOT with Home Brew. The ROOT command line tools work fine (I get graphs). Xcode does not give errors in the code (red bars). But there are 6 errors in "ROOT_basic":
error build: Undefined symbol: TApplication::TApplication(char const*, int*, char**, void*, int)
error build: Undefined symbol: TVersionCheck::TVersionCheck(int)
error build: Undefined symbol: TGraph::TGraph(int, double const*, double const*)
error build: Undefined symbol: TCanvas::TCanvas(char const*, char const*, int)
error build: Undefined symbol: TObject::operator delete(void*)
error build: Undefined symbol: TStorage::ObjectAlloc(unsigned long)
Edit: It turned out to be a install problem. I uninstalled Root via Brew and installed it again with Macports. Now it works

Eclipse g++ not found in path: windows

I am attempting to setup SDL2 for C++ with Eclipse on Windows 7.
In order to do so, I am following the tutorial in this link, which states that I must first install MinGW. So I follow the link provided in order to setup MinGW. I follow all the steps without issue. I then open Eclipse and attempt to build a simple hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
To my surprise, this code doesn't build, with 6 errors.
I then proceed to simplify the program further:
int main()
{
return 0;
}
This also does not compile. There are two errors:
Program "g++" not found in PATH
Program "gcc" not found in PATH
Here is a screenshot.
However, my path does contain "C:\mingw\bin". I have also tried changing this to "C:\mingw". Looking inside "C:\mingw\bin", I find gcc and g++:
In addition, compiling a test program using the command line (g++ Test.cpp -o Test) works just fine, as does "g++ -v".
I have been searching the web for hours, and can't seem to find an answer as to why Eclipse can't seem to compile anything with MinGW. Questions I have looked at on SO (which haven't been able to fix my issue) include:
Eclipse mingw binary not found
Eclipse not finding c std libraries
g++ not found in path
Eclipse C++ : "Program g++ not found in PATH"
Program g++ not found in path
Program g++ not found in path C++ [duplicate]
Eclipse CDT (Juno) in Win7: Cannot find g++ in PATH, iostream unresolved and other fun stuff
Additional info:
Window > Preferences > C/C++ > Build > Settings > "CDT GCC Built-in Complier Settings MinGW [Shared]" : Toolchain MinGW GCC is not detected on this system.
I have also reinstalled Eclipse to no avail.
I realize that this may be a duplicate question of some that I have linked, but the information in previous questions have not been able to fix my problem, and I fear that adding a comment to an old question may not result in an answer.
Please request additional information as needed.
You need to set the environment for the c/c++ builder.
First you need to install the GNU tool-chain, you can choose either MinGW or Cygwin. You can see the steps here. I used MinGW.
Go to Window->Preferences->C/C++->Build->Environment and add a new variable, name it whatever you want for example a named it "MINGW", now paste the binaries directory of MinGW which is by default C:\MinGW\bin, you should have something like this:
Now when you create a new project you just have to select the MinGW tool-chain:
Hope that helps.
It appears as though I have fixed the problem for the moment.
In case others encounter the same issue:
Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Command changed from "g++" to "C:\mingw\bin\g++".

Compilation error: "stddef.h: No such file or directory"

Whenever I try to compile this code it always ends up with this error:
In file included from /usr/include/wchar.h:6:0,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/cwchar:44,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/postypes.h:40,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iosfwd:40,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ios:38,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ostream:38,
from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iostream:39,
from test.cpp:1:
/usr/include/sys/reent.h:14:20: fatal error: stddef.h: No such file or directory
#include <stddef.h>
^
compilation terminated.
The code I was trying to compile is:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! :D";
return 0;
}
The error is because your gcc-core package and gcc-g++ are not of the same version. Either downgrade one of them to solve the problem or update both the libraries. Updating both the libraries is the recommended way.
I had this error on a fresh MinGW install, it had nothing to do with the installed packages mentioned in the current accepted answer by "Prasanth Karri". In my case the issue was caused by -nostdinc in my Makefile. I actually only needed that compiler flag when building for a different target platform (not when using MinGW) so I fixed the issue by removing that flag from MinGW builds.
When I was incorporating a software library written in C into an existing demo project(used a C++ mbed library) I encountered this problem. The demo project would compile just fine, but after I replaced the existing main file by my own, this error occurred.
At this point I hadn't yet thought about the fact that the mbed library that I needed was written in C++. My own main file was a .c file that #include the mbed header file. As a result I used my normal C source as if it was a C++ source. Therefore the compiler that was used to compile my main file was the C compiler.
This C compiler then encountered a #include of a module that actually does not exist (within its scope), as it's not a C++ compiler.
Only after I inspected the output of the build log I realised the various source C and C++ files were compiled by more that 1 compiler(the c++ compiler). The project used used compilers arm-none-eabi-c++ and arm-none-eabi-gcc (for embedded systems) as seen below.
Compile log:
Building file: ../anyfile.cpp
Invoking: MCU C++ Compiler
arm-none-eabi-c++ <A lot of arguments> "../anyfile.cpp"
Finished building: ../anyfile.cpp
Building file: ../main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc <A lot of arguments> "../main.c"
In file included from <Project directory>\mbed/mbed.h:21:0,
from ../main.c:16:
<Project directory>\mbed/platform.h:25:19: fatal error: cstddef: No such file or directory
compilation terminated.
Of course in a C++ environment cstddef exists, but in a C environment cstddef doesn't exist, in stead it's just C's implementation of stddef.
In other words, cstddef does not exist in the C compiler.
I resolved this problem by renaming my main.c file to main.cpp and the rest of the code compiled smoothly too.
TLDR/Conclusion: When building a C++ project, avoid mixing C files with C++ files(sources and headers). If possible rename .c files to .cpp files to use the C++ compiler in stead of the C compiler where required.
In order to update it, follow below.
If you are on Windows, just run these on command prompt or powershell
Update the package list: mingw-get update
After updating the package list, run: mingw-get upgrade
Source: How to update GCC in MinGW on Windows?
This problem was solved for me as I installed codeblocks with mingw compiler then I copied the mingw folder from codeblocks to C drive and added
C\mingw\bin to the environment variables.
If you try to compile and see a message like, "fatal error: stddef.h: No such file or directory", the error is because your gcc-core and gcc-g++ packages are not of the same version. Rerun the Cygwin install and make sure that you select the highest numbered versions of gcc-core and gcc-g++.
After installing the C++ compiler with MinGW I encountered this problem as well. Apparently, you have to also install mingw32-base. Go to C:/MinGW/bin/mingw-get.exe (my path) and check it for installation at the Basic Setup tab.

Setting up MingW and Code::Blocks in Windows 8 64 bit

I am trying to setup MingW and Code::Blocks on my Windows 8 64 bit laptop, and I'm facing some problem while building a main.cpp file. These are the versions that I have installed:
x86_64-w64-mingw32-gcc-4.7.4-release-win64_rubenvb.7z for MingW (4th one in that list), and
codeblocks-12.11-setup.exe for Code::Blocks.
I've set the path to mingw64\bin in the environment variable. Also, in the Code::Blocks compiler settings, I have set path for all ToolChain Executables. There are two gcc in the MingW bin path. One is - gcc.exe and other is - x86_64-w64-mingw32-gcc.exe. Same for C++ Compiler, Linker for static and dynamic libs. Now when I try to build a simple Hello World file, it shows some errors:
obj\Debug\main.o -- In function swscanf
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
obj\Debug\main.o -- In function `wscanf':
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
Similarly there are many errors and undefined references for - fwprintf, wprintf, std:cout, etc..
I'm sure there is some configuration problem, but I cannot find out what's the issue. Can any one take a look at the problem.
Here's the code I'm running:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
If you don't require 64 bit support, consider downloading "codeblocks-12.11mingw-setup.exe"
It packs its own 32 bit mingw and has everything preconfigured for you.
Otherwise, if you still need to compile amd64 apps:-
Assuming you extracted to D:\Mingw64, under the "Tool Chain Executables" tab, set the "Compiler's Installation Directory" to "D:\Mingw64\bin", Then click auto-detect to check whether code::blocks likes what it finds.
If everythings Ok, make sure the rest of the fields are as follows:-
C compiler: x86_64-w64-mingw32-gcc.exe
C++ compiler: x86_64-w64-mingw32-g++.exe
Linker for dynamic libs: x86_64-w64-mingw32-g++.exe
Linker for static libs: x86_64-w64-mingw32-ar.exe
Debugger: GDB/CDB Debugger...
Ressource compiler: x86_64-w64-mingw32-windres.exe
Make program: mingw32-make.exe
Click on the "Additional Paths" tab and enter the following line:- (the path is correct for ver 4.7.4,the one you downloaded. It ends with 4.7.3)
D:\Mingw64\libexec\gcc\x86_64-w64-mingw32\4.7.3\
Click on the "Search directories" tab to the imediate left "Toolchain Executables"
Add the following paths:-
D:\Mingw64\include
D:\Mingw64\x86_64-w64-mingw32\include
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\backward
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\x86_64-w64-mingw32
D:\Mingw64\lib\gcc\x86_64-w64-mingw32\4.7.3\include
Click on the "linker" tab and add the following paths:-
D:\Mingw64\Lib
D:\Mingw64\x86_64-w64-mingw32\lib
And finally click on the "Compiler Settings tab", Click on "Other options" and add:-
-m64
One last thing-> remember to save those settings to disk!
If it crashes while you test this new config, you might wish you had!
Do this (I think) either by "File->Save Everything" or restarting code::blocks ...without another instance running.
Try compiling something then share the outcome
Peace! Dear brother, I think you need this:
(1)
std::cout << "Hello world!" << std::endl;
You need to put "std::function_name" whenever you are using a predefined function from iostream.h, because this is you way access it in GCC, which Code::Blocks uses.
(2) Try it compiling by omitting "using namespace std;" from your code.
Because I've compiled C and C++ code many times in Code::Blocks on 32-bit Windows XP, and it did!
(3) Make sure the path is set to the directory "mingw\bin".
(4) Ensure if you have 64-bit MinGW installed and path set to its "bin" directory (if you want to compile for 64-bit). It will compile for 64-bit by default.
(5) Try the flags/arguments "-m32" and "-m64" for compiling for 32- and 64-big respectively.

C++; eclipse linker error

So working on getting my eclipse IDE going so I can develop my arduino uno in eclipse.
My C++ is weak so this is probably a nube error on my part.
I have a blink program that looks for an arduino library I compiled from the arduino IDE's library.
My code points to the header file and my code find it fine; meaning I can click on:
#include <arduino.h>
and go view the header
this: "C:/programs/arduino-1.0/hardware/arduino/cores/328p_lib/libuno_library.a"
is a valid path... but I get the following error:
>****** Build of configuration Debug for project project1 ****
>make all
>Building target: project1.elf
>Invoking: AVR C++ Linker
>avr-g++ -Wl,-Map,project1.map,--cref -L"C:\programs\arduino->1.0\hardware\arduino\cores\328p_lib" -mmcu=atmega328p -o "project1.elf" ./code/code1.o >-l"C:/programs/arduino-1.0/hardware/arduino/cores/328p_lib/libuno_library.a"
>c:/programs/winavr/bin/../lib/gcc/avr/4.3.3/../../../../avr/bin/ld.exe: cannot find ->lC:/programs/arduino-1.0/hardware/arduino/cores/328p_lib/libuno_library.a
>make: *** [project1.elf] Error 1
>**** Build Finished ******
Well after wasting 2 days or so of fun time I finally found the problem.
http://sourceforge.net/projects/avr-eclipse/forums/forum/664382/topic/4640554
When adding the static library to the linker you have to remove the lib prefix and the .a suffix. not sure what that is about.
Right click on the project>Click on C/C++ BUild> Settings > GCC C++ Linker> Libraries
Click the first icon Add> Add the library name ( without the .a suffix, the suffix will be added automatically)
This will ensure that the library is added to the project.
If the library is part of another project >Go to GCC C Compiler> directories >Add the directory
This will ensure that the library is there for getting the compilation done.