I am building a Makefile project in Eclipse Juno, and I have it set up so it compiles and debugs (it's using CMake, so I'm not using the internal tools). However, Eclipse obviously hasn't been informed of the right headers, as in the following code:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world << endl;
return 0;
}
the include "iostream" and symbols "std", "cout" and "endl" are all unresolved.
How should I make Eclipse aware of these so it will stop underlining everything in red and spamming with errors?
This can be resolved by specifying the following environmental variables in Project->Properties->C++ Build->Environment.
LANG=en_US
LC_ALL=en_US
Apparently they are needed for the auto-discovery tools to work out where the includes live.
Answer gleaned from this Eclipse forum thread.
Related
I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.
not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4
I'm running on a CentOS 6.5 x64 OS and have used yum to install armadillo. I'm developing in Eclipse CDT
I've included the armadillo header in the project properties >> C/C++ Build >> Settings >> GCC C++ Compiler >> Includes >> Include files. The entry is: "/usr/include/armadillo"
The header file I am working on recognizes armadillo and the include statement isn't flagged for any errors or warnings.
Below is the code:
#include <armadillo>
using namespace std;
using namespace arma; // arma is not recognized as a symbol
const double DEGREES_PER_RADIAN = 180.0 / datum::pi; // datum is not recognized
I've checked the file /usr/include/armadillo and it does include the namespace arma section.
//! \namespace arma namespace for Armadillo classes and functions
namespace arma
{
// preliminaries
...
I've also checked the permissions and the /usr/include/armadillo file is readable to all users.
The problem is when I add the "using namespace arma", CDT marks it as an error and says that "Symbol 'arma' could not be resolved".
At this point, I don't have any other ideas to figure out why the namespace isn't recognized. Any insights or pointers to figure this out would be much appreciated.
This question's answer provided the answer to my issue:
Clean Eclipse Index, it is out of sync with code
Josh Kelley's answer from the linked issue:
Right-click on your project, go under the Index submenu, and choose either "Rebuild," "Update with modified files," or "Freshen all files."
I don't know the difference between those three options, but one of "Update with modified files" or "Freshen all files" usually fixes it for me.
Also, I'm sure you've already done this, but make sure that you're running the latest version of the Eclipse CDT. Current versions seem to have much more reliable indexing than previous versions.
After running Index >> Rebuild and Index >> Freshen All Files, the errors were gone.
The problem occurs when trying to use CDT for editing files in OpenFOAM, a popular computational fluid dynamics software package. I am using Eclipse Kepler SR2 Build id: 20140224-0627 and CDT 8.3.0.201402142303 The following is a simplified version of the problem.
Given the following hello.cpp
#include <iostream>
#include <string>
int main(){
#include "hello.H"
std::cout << hello << std::endl;
}
and the following hello.H
std::string hello("Hello world!");
Indexing can't resolve hello in hello.cpp. However, if I change hello.H to hello.inc it does work. This isn't a feasible solution for OpenFOAM. How could I make the indexing and autocomplete work with the given structure?
Note: I did try Eclipse Luna too, and the same happened.
Edit: I think this might be a bug. After more experimenting, the following happened. When I named the hello.H to hello.inc, the hello in hello.cpp was recognized. However, when I rebuild index it wasn't. Then I resaved hello.inc without functional changes, and behold, it was recognized again. However, if I turned off automatic indexing it no longer got resolved after rebuilding the index.
Edit 2: After further research on the problem, I found the following duplicates, without the random behavior:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=418085
and to my shame:
#include inside function body doesn't work (CDT/Eclipse C++)
Thus this question can be closed as a duplicate. To those interested, CDT indexer is not actually designed to do this, as this is bad coding style. The main message to me was that I should use some other IDE with OpenFoam.
I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. Here's the code.
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
int main()
{
pqxx::connection MyConn ("dbname=dbESM user=postgres");
cout << "Hello world!" << endl;
return 0;
}
But I get the following error on hitting F9 to compile and run:
/usr/include/pqxx/connection.hxx|87|undefined reference to
`pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'
The above message is from the file connection.hxx and the line highlighted is this:
explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}
The connection.hxx file is not mine - I think it's part of pqxx.
I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.
You need to add the reference to the libpqxx library to the project.
Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options, then open the tab called Linker settings, then hit Add, then enter pqxx.
If you were using the libpq C library instead, the procedure would be identical except the name would be pq.
You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.