compiling xerces-c++ program with Eclipse - c++

I am trying to program with xerces-c on windows.
I have successfully built the library and compiled a simple program with success, barebone, with just cmd and notepad. However when I tried to move things to eclipse, things got a bit out of hand.
By simply having a c++ helloworld sample, then including the include files into the project path and build, eclipse is refusing to build the project, generating a lot of error, which I think mostly related to the namespace.
The errors goes as follow:
Info: Internal Builder is used for build
g++ -I...blablabla -O3 -Wall -c -fmessage-length=0 -o "src\\helloworld.o" "..\\src\\helloworld.cpp"
gcc -O3 -Wall -c -fmessage-length=0 -o "src\\includes\\xercesc\\util\\RefStackOf.o" "..\\src\\includes\\xercesc\\util\\RefStackOf.c"
..\src\includes\xercesc\util\RefStackOf.c:30:1: error: unknown type name 'XERCES_CPP_NAMESPACE_BEGIN'
XERCES_CPP_NAMESPACE_BEGIN
..\src\includes\xercesc\util\RefStackOf.c:35:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
template <class TElem>
^
..\src\includes\xercesc\util\RefStackOf.c:44:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
template <class TElem> RefStackOf<TElem>::~RefStackOf()
..\src\includes\xercesc\util\RefStackOf.c:...
...this and that, this and that.... and finally...
..\src\includes\xercesc\util\RefStackOf.c:160:1: error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
XERCES_CPP_NAMESPACE_END
and if I removed the file in error, error will just pop up to another file with the same format, beginning with "I dunno what XERCES_CPP_NAMESPACE_BEGIN means"
I have also tried using another builder, say mingw32-make, but it also generates error in the same format. Only changing the title a bit, and perhaps the files are compiled in different order, starting with this:
mingw32-make all
'Building file: ../src/includes/xercesc/util/BaseRefVectorOf.c'
'Invoking: GCC C Compiler'
gcc -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/includes/xercesc/util/BaseRefVectorOf.d" -MT"src/includes/xercesc/util/BaseRefVectorOf.o" -o "src/includes/xercesc/util/BaseRefVectorOf.o" "../src/includes/xercesc/util/BaseRefVectorOf.c"
../src/includes/xercesc/util/BaseRefVectorOf.c:24:1: error: unknown type name 'XERCES_CPP_NAMESPACE_BEGIN'
XERCES_CPP_NAMESPACE_BEGIN
I am guessing that the build program does not understand how to replace the
XERCES_CPP_NAMESPACE_BEGIN, with
namespace XERCES_CPP_NAMESPACE { }
namespace xercesc = XERCES_CPP_NAMESPACE;
But I don't know of a way to teach the builder how to do this, nor I am sure if I have compiled the library in the correct way.
Can someone point me in some direction as to how to solve this? I can compile a simple program by just using cmd, so certainly I should be able to do it in Eclipse.
background:
OS: Windows 8 64bit
compiler: mingw-w64 5.3.0 posix-seh-rev0
lib compiled with msys
lib compilation command:
./configure --prefix=/specific-location --host=x86_64-w64-mingw32 --enable-netaccessor-winsock --enable-transcoder-windows --disable-pretty-make
make LDFLAGS=-no-undefined
make check
make install
cmd compilation command: g++ -Llib -Iinclude -o b.exe test.cpp
so you can see that I have also included every xerces-c header into the compiler with the -Iinclude command, so I reckon that g++ should not produce error when invoked in Eclipse, not that I know anything if its gcc.
simple program that ran when simply compiled with cmd:
//test.cpp
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <iostream>
using namespace std;
using namespace xercesc;
int main (int argc, char* args[]) {
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n"
<< message << "\n";
XMLString::release(&message);
return 1;
}
XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true); // optional
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(errHandler);
char* xmlFile = "x1.xml";
try {
parser->parse(xmlFile);
DOMDocument* xmlDoc = parser->getDocument();
DOMElement* elementRoot = xmlDoc->getDocumentElement();
if( !elementRoot ) throw(std::runtime_error( "empty XML document" ));
DOMNodeList* children = elementRoot->getChildNodes();
const XMLSize_t nodeCount = children->getLength();
cout << nodeCount << " nodes\n";
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return -1;
}
delete parser;
delete errHandler;
return 0;
}
#EDIT
After further investigation, it seems that the XERCES_CPP_NAMESPACE_BEGIN is handled in preprocessor, but its only defined in the file util/XercesDefs.hpp
In the files with compilation error, they always begin with
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefStackOf.hpp> //or include anything else blablabla, which ultimately leads to XercesDefs.hpp
#endif
I searched through the entire build directory for the string XERCES_TMPLSINC, it was contained in 44 .c or .hpp files, but everyone of them is #if !defined(XERCES_TMPLSINC) <===== WRONGWRONG , so like XERCES_TMPLSINC was never actually defined.
According to some forum post, XERCES_TMPLSINC was required for some old c compilers, so does anyone know how to fix this in my build? how could I define XERCES_TMPLSINC in the project? I have tried adding #define XERCES_TMPLSINC to the helloworld file but it still does not work.
#EDIT
my bad, actually all the .c files contained #if defined(XERCES_TMPLSINC) and all hpp files were #if !defined(XERCES_TMPLSINC), this definitely seems a c and c++ thing?

I was able to compile it in Ecliplse (on Linux) by adding the following preprocessor defs in g++:
-DXERCESC_INCLUDE_GUARD_WEAVEPATH_CPP -DXERCES_HAVE_STDINT_H -DXERCES_TMPLSINC.
On Windows I think you should substitute the with
-DXERCES_HAVE_STDINT_H -DXERCES_HAVE_CSTDINT

Related

Cannot use auto keyword in VS Code while coding in C++

I am using VS Code to code C++. It performs totally fine. But whenever I use auto keyword in my code, the program simply fails to Compile.
For example, to iterate over a string my code without using auto keyword will look like
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(int i=0;i<s.length();i++)
{
cout<<s.at(i)<<' ';
}
cin.get();
}
It compiles find and runs giving Correct Output.
Executing task: g++ -g -o helloworld helloworld.cpp
Terminal will be reused by tasks, press any key to close it.
OUTPUT : H e l l o
But whenever I try to perform the same job but using auto keyword, the code looks like
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(auto c:s)
{
cout<<c<<' ';
}
cin.get();
}
But it gives Compile Time Error
Executing task: g++ -g -o helloworld helloworld.cpp
helloworld.cpp: In function 'int main()':
helloworld.cpp:7:14: error: 'c' does not name a type
for(auto c:s)
^
helloworld.cpp:11:5: error: expected ';' before 'cin'
cin.get();
^
helloworld.cpp:12:1: error: expected primary-expression before '}' token
}
^
helloworld.cpp:12:1: error: expected ')' before '}' token
helloworld.cpp:12:1: error: expected primary-expression before '}' token
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Please help me out.
This is the clue:
Executing task: g++ -g -o helloworld helloworld.cpp
I suspect you need to compile with -std=c++11 or later.
Older builds of gcc/g++ will default to C++ 98 standard before the auto keyword was introduced. There might be other configurations where this is the default as well. The workaround is simple.
Configure your build such that the task being compiled is this:
g++ -std=c++11 -g -o helloworld helloworld.cpp
You can also use -std=c++14 or -std=c++17 if available.

Systemc Error with the library

I installed the SystemC library 2.3.1 using this tutorial.
I wrote this hello world example:
//hello.cpp
#include <systemc.h>
SC_MODULE (hello_world) {
SC_CTOR (hello_world) {
}
void say_hello() {
cout << ”Hello World systemc-2.3.0.\n”;
}
};
int sc_main(int argc, char* argv[]) {
hello_world hello(“HELLO”);
hello.say_hello();
return(0);
}
and compiled with this command:
export SYSTEMC_HOME=/usr/local/systemc230/
g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux -Wl,-rpath=$SYSTEMC_HOME/lib-linux -o hello hello.cpp -lsystemc -lm
When I compile the code, I got a error with the library:
In file included from hello.cpp:1:0:
/usr/local/systemc230/include/systemc.h:118:16: error: ‘std::gets’ has not been declared
using std::gets;
^~~~
How can I solve this?
std::gets has been removed in C++11 (See What is gets() equivalent in C11?)
If you're building using C++11 flag (maybe with a g++ alias), you have to disable this line in systemc.h.
Replace
using std::gets;
with
#if defined(__cplusplus) && (__cplusplus < 201103L)
using std::gets;
#endif
As guyguy333 mentioned, in new versions, g++ is an alias for C++11.
so adding -std=c++98 would solve the problem.
The compile command may like
$ g++ -std=c++98 -lsystemc -pthread main.cpp -o main
You seem to have copy pased the code from webpage as it is. Please remember “” and "" are not the same thing. On line 8
cout << ”Hello World systemc-2.3.0.\n”;
replace it with
cout << "Hello World systemc-2.3.0.\n";
and on line 13
hello_world hello(“HELLO”);
replace it with
hello_world hello("HELLO");
And then execute the code again.
GoodLuck.

Why does my c++ compiler seem to be compiling in c

I am using geany (code::blocks wouldnt run my programs) as a compiler to compile a simple c++ program with one class. I am on Linux Mint 17 on a Dell Vostro 1500. Compiling works fine with both .cpp files, but the header file gives this error:
gcc -Wall "Morgan.h" (in directory: /home/luke/Documents/Coding/Intro#2)
Morgan.h:5:1: error: unknown type name ‘class’
class Morgan
^
Morgan.h:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
Compilation failed.
This is the main.cpp :
#include <iostream>
#include "Morgan.h"
using namespace std;
int main()
{
Morgan morgObject;
morgObject.sayStuff();
return 0;
}
This is the Header file (Morgan.h):
#ifndef MORGAN_H
#define MORGAN_H
class Morgan
{
public:
Morgan();
void sayStuff();
protected:
private:
};
#endif // MORGAN_H
And this is the class (Morgan.cpp):
#include <iostream>
#include "Morgan.h"
using namespace std;
Morgan::Morgan()
{
}
void Morgan::sayStuff(){
cout << "Blah Blah Blah" << endl;
}
I really do not know what is going wrong, so any help would be appreciated. I copy and pasted the same code into a windows compiler and it worked fine, so it might just be the linux.
also when I run the main.cpp this is what shows:
"./geany_run_script.sh: 5: ./geany_run_script.sh: ./main: not found"
You don't compile .h files. Try g++ -Wall main.cpp Morgan.cpp
Your issue is that you are compiling C++ code with a C compiler (GCC). The command you are looking for is g++. The complete command that would compile your code is:
g++ -Wall -o run.me main.cpp Morgan.cpp
If a file is included (In your case the Morgan.h file, you do not need to explicitly compile it. )

Programming Error, New to C++

I'm taking an intro class into C++ and we are using jgrasp. I'm doing a simple exercise right now which looks like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Testing 1, 2, 3\n";
return 0;
}
though when I compile it and link it I get this error:
----jGRASP exec: g++ -c -fsyntax-only H:\COP2334\Excerises\Display 1.10
g++.exe: warning: H:\COP2334\Excerises\Display 1.10: linker input file unused because linking not done
----jGRASP: operation complete.
The option -c means "Compile or assemble the source files, but do not link."
So, of course, linking isn't performed.

Getting compiler to work in Notepad++

I'm having some trouble using Notepad++ to compile code. I've installed notepad++ (and NppExec), downloaded MinGW from this source (http://nuwen.net/mingw.html) and installed it to "C:\MinGW\".
Then I tried to set notepad++ to use g++ to compile c++. Per advice, I entered the following into NppExec's console:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++.exe -g "$(FILE_NAME)"
Saved it as C++ Compiler, and added it to the "Macros" section of the toolbar.
Then I tried to run a simple test program:
#include <iostream>
int main()
{
cout << "Hello, world!";
}
After that a couple of weird errors popped up. First it wanted me to save to System32 by default, which I don't remember it doing before (it won't let me, forcing me to save in Documents).
I let it save to documents, than tried to run it with the compiler. It gives me this error, which I don't recognize at all:
NPP_EXEC: "C++ Compiler"
NPP_SAVE: C:\Users\Bova\Documents\Test.cpp
CD: C:\Users\Bova\Documents
Current directory: C:\Users\Bova\Documents
C:\MinGW\bin\g++.exe -g "Test.cpp"
Process started >>>
Test.cpp: In function 'int main()':
Test.cpp:5:5: error: 'cout' was not declared in this scope
cout << "Hello, world!";
^
Test.cpp:5:5: note: suggested alternative:
In file included from Test.cpp:1:0:
c:\mingw\include\c++\4.8.2\iostream:61:18: note: 'std::cout'
extern ostream cout; /// Linked to standard output
^
<<< Process finished. (Exit code 1)
Please help.
There is nothing wrong with your compiler. You are not using the correct namespace to use cout
#include <iostream>
int main()
{
std::cout << "Hello, world!";
}
Or
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
}