libtorque - how do i include the PBSD_status function? - c++

I am writing an application to test whether pbs_connect() is working or not. Here is my code:
#include <stdio.h>
#include "/usr/include/torque/pbs_ifl.h"
#include "/usr/include/torque/pbs_error.h"
#include <pbs_config.h>
#include "libpbs.h"
int main() {
printf("Hello world\n");
int server = pbs_connect("inferno.local");
//batch_status * stat1 = pbs_statserver(server, NULL, NULL);
pbs_errno = 0;
batch_status * stat1 = PBSD_status(server, 21, (char *)"", NULL, NULL);
printf("fd: %d\n", server);
//printf("text: %s\n", stat1->text);
//printf("name: %s\n", stat1->name);
printf("name: %d\n", pbs_errno);
return 0;
}
//compiled using - //g++ -o test test.c -L/usr/lib64 -ltorque
I get:
# g++ -o test test.c -L/usr/lib64 -ltorque
test.c:7:24: error: pbs_config.h: No such file or directory
test.c:8:20: error: libpbs.h: No such file or directory
test.c: In function 'int main()':
test.c:19: warning: deprecated conversion from string constant to 'char*'
test.c:24: error: 'PBSD_status' was not declared in this scope
The source file that contains PBSD_status can be found here:
https://github.com/adaptivecomputing/torque/blob/4.2.7/src/lib/Libifl/PBSD_status.c
Is there something i need to include in my g++ command to get this to work? I have checked in /usr/lib64/, and there is no libpbs.h or pbs_config.h. Where would they be, if they aren't there?

As far as your headers, you're running into the difference between installed and not installed headers. Essentially, a software project doesn't install every header inside that project, only relevant ones for the API. These other two aren't in the API and therefore aren't installed. You need to reference their path.
As far as including PBSD_status() in the library, you could edit the Makefile for Libpbs to include the source file for PBSD_status() and then rebuild, or you could link to the libifl library, which is in src/lib/Libifl from the base directory for the project.

Related

extern main declaration from bsplib returns error

Im setting up the bsplib (https://github.com/Zefiros-Software/BSPLib) on a windows system (in VS Code) using WSL. When compiling I get the error message:
test.cpp:4:5: error: conflicting declaration of C function ‘int main()’
4 | int main()
| ^~~~
In file included from /mnt/d/study/software/bsp/include/bsp/bspExt.h:30,
from /mnt/d/study/software/bsp/include/bsp/bsp.h:34,
from test.cpp:2:
/mnt/d/study/software/bsp/include/bsp/bspClass.h:59:12: note: previous declaration ‘int main(int, char**)’
59 | extern int main(int argc, char **argv);
The program is used is just a bare example for BSP:
#include <iostream>
#include "bsp/bsp.h"
int main()
{
bsp_begin(bsp_nprocs());
int s = bsp_pid();
int p = bsp_nprocs();
printf("Hello World from processor %d / %d", s, p);
bsp_end();
return 0;
}
Compiled with:
g++ -I/mnt/d/study/software/bsp/include -g -lpthread -o main test.cpp
To my (quite limited) knowledge, the 'extern' in the header file should prevent the compiler from labelling the main as 'duplicate' of some sort. Im mostly interested in some of BSPs functionalities as part of a class of mine, that sadly does not include any support on the installation. What I've done so far:
Copied the include files from the repo
Added the include path to the compilation (-I Flag) and the -lpthread as instructed by the class script
Added the include path to the configuration (c_cpp_properties.json) [tested both with and without this, no difference]
Due to the many possible sources of that error (program, compiler, wsl, library, configuration, vs code, my stupidity) I cant determine where I am mistaken, nor am I able to find online resources to that combination.

Problems compiling C++ programs with wineg++/winelib

I am having trouble compiling C++ programs with wineg++. To illustrate my problem, I have written two test programs.
msgbox.cpp
#include <algorithm>
#include <iterator>
#include <cstdio>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
char buf[30], *pos = buf;
int xs[] = {1,3,2,4,3,5,4,6,5,7,6,8,7,9};
std::sort( std::begin(xs), std::end(xs) );
for (int x : xs) {
pos += std::sprintf(pos, "%d ", x);
}
MessageBox(0, buf, "Hello", 0);
return 0;
}
frame.cpp
#include "../win32xx/include/wxx_wincore.h"
#include "../win32xx/include/wxx_frame.h"
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
CWinApp winApp;
CFrame frame;
CWnd view;
frame.SetView(view);
frame.Create();
winApp.Run();
}
The second program uses the Win32++ library, which I can't recommend enough.
Both programs compile and run just fine using a cross-compiler:
okuu% x86_64-w64-mingw32-g++ msgbox.cpp -o msgbox.exe
okuu% wine ./msgbox.exe
okuu% x86_64-w64-mingw32-g++ frame.cpp -o frame.exe -lgdi32 -lcomctl32 -static
okuu% wine ./frame.exe
okuu% rm *exe*
But I want to use winelib so that I can use both the Windows API and Unix libraries. This is what I tried first:
okuu% wineg++ msgbox.cpp -o msgbox.exe
okuu% ./msgbox.exe
okuu% wineg++ frame.cpp -o frame.exe -mwindows
In file included from ../win32xx/include/wxx_appcore.h:57:0,
from ../win32xx/include/wxx_wincore.h:96,
from frame.cpp:1:
../win32xx/include/wxx_appcore0.h:120:12: fatal error: process.h: No such file or directory
#include <process.h>
^~~~~~~~~~~
compilation terminated.
winegcc: g++ failed
Then I read wineg++'s man page, which says:
-mno-cygwin
Use Wine implementation of MSVCRT, instead of linking against the host system libc. This is necessary for the vast majority of Win32 applications, as they typically depend on various features of MSVCRT. This switch is also used by the MinGW compiler to link against MSVCRT on Windows, instead of linking against Cygwin libc. Sharing the syntax with MinGW makes it very easy to write Makefiles that work under Wine, MinGW+MSYS, or MinGW+Cygwin.
So I tried again with -mno-cygwin, and got a 2000-line error message that begins with:
okuu% wineg++ frame.cpp -o frame.exe -mwindows -mno-cygwin
In file included from /usr/include/c++/7.2.1/cstdlib:75:0,
from /usr/include/c++/7.2.1/bits/stl_algo.h:59,
from /usr/include/c++/7.2.1/algorithm:62,
from ../win32xx/include/wxx_appcore0.h:110,
from ../win32xx/include/wxx_appcore.h:57,
from ../win32xx/include/wxx_wincore.h:96,
from frame.cpp:1:
/usr/include/stdlib.h:310:5: error: ‘int32_t’ does not name a type; did you mean ‘wint_t’?
int32_t *fptr; /* Front pointer. */
^~~~~~~
wint_t
/usr/include/stdlib.h:311:5: error: ‘int32_t’ does not name a type; did you mean ‘wint_t’?
int32_t *rptr; /* Rear pointer. */
^~~~~~~
wint_t
/usr/include/stdlib.h:312:5: error: ‘int32_t’ does not name a type; did you mean ‘wint_t’?
int32_t *state; /* Array of state values. */
^~~~~~~
wint_t
/usr/include/stdlib.h:316:5: error: ‘int32_t’ does not name a type; did you mean ‘wint_t’?
int32_t *end_ptr; /* Pointer behind state table. */
^~~~~~~
wint_t
/usr/include/stdlib.h:320:8: error: ‘int32_t’ has not been declared
int32_t *__restrict __result) __THROW __nonnull ((1, 2));
^~~~~~~
So it seems C99's fixed-size integer types are not available. That seems easy enough to solve:
frame.cpp
#include <stdint.h>
#include "../win32xx/include/wxx_wincore.h"
#include "../win32xx/include/wxx_frame.h"
// etc. etc. etc.
And I tried again, but got a different 2000-line error message that begins with:
okuu% wineg++ frame.cpp -o frame.exe -mwindows -mno-cygwin
In file included from /usr/include/c++/7.2.1/cwchar:44:0,
from /usr/include/c++/7.2.1/bits/postypes.h:40,
from /usr/include/c++/7.2.1/bits/char_traits.h:40,
from /usr/include/c++/7.2.1/string:40,
from ../win32xx/include/wxx_appcore0.h:111,
from ../win32xx/include/wxx_appcore.h:57,
from ../win32xx/include/wxx_wincore.h:96,
from frame.cpp:2:
/usr/local/include/wine/msvcrt/wchar.h:398:23: error: conflicting declaration of C function ‘size_t mbstowcs(wchar_t*, const char*, size_t)’
size_t __cdecl mbstowcs(wchar_t*,const char*,size_t);
^~~~~~~~
At this point I have run out of ideas. This is what I have understood so far:
My system's libc and Wine's MSVCRT have conflicting definitions. (This was probably to be expected.)
My system's libc++ is hardwired to work with my system's libc.
Wine comes with a MSVCRT, but not with a C++ standard library implementation.
The logical course of action with the information I have so far would be to look for a C++ standard library implementation that's compatible with Wine's MSVCRT, but I don't know of one. Does anybody here know of one?
The only solution I can think of is to stick with the system libc and write your own process.h. This file should either #include the standard header files that have the functions Win32++ needs or provide its own implementations of those functions. If Win32++ won't compile without a particular function but your program does not actually depend on that function, the implementation of that function can simply return 0 or another fake value.
If the system libc has a header file that Win32++ asks for, but the file does not declare all of the functions that Win32++ expects, you'll have to write a header file such as win32xx-compat.h that defines those functions and #include it before any Win32++ header.

shared library with Undefined Behavior Sanitizer (clang++)

I have a problem while running an executable file with dlopen function used to open shared and sanitized library with a one simple function.
I use precompiled Clang 3.9.0 for Ubuntu 14.04.
My question is: Is it possible to run it properly, so I can look for undefined behavior errors in the library while running an executable ? If the answers is yes, then how ?
I have two files:
//simpledll.cpp
#include <cstdio>
int hehe(int argc) {
int k = 0x7fffffff;
k += argc;
return 0;
}
//dlopen.cpp
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
void* handle;
handle = dlopen("simpledll.so", RTLD_LAZY);
if(!handle) {
fprintf(stderr, "%s\n", dlerror());
}
int (*function)(int) = reinterpret_cast<int (*)(int)> (dlsym(handle, "_Z4hehei"));
if (function == nullptr)
fprintf(stderr, "Nope\n");
else
function(1000); // this yields signed integer overflow
return 0;
}
I have tried to get it to work in two steps (both have failed)
Step I
Compile the executable with:
clang++ dlopen.cpp -ldl --std=c++11 -o dlopen
Compile the library with:
clang++ -fsanitize=undefined -shared -o simpledll.so -fPIC simpledll.cpp
Result:
./dlopen: symbol lookup error: simpledll.so: undefined symbol: __ubsan_handle_add_overflow
Step II (idea from this forum)
Compile the executable as in Step I,
Compile the library with:
clang++ -fsanitize=undefined -shared -Wl,--whole-archive -L/usr/local/lib/clang/3.9.0/lib/linux/ -lclang_rt.ubsan_standalone_cxx-x86_64 -Wl,--no-whole-archive -lclang_rt.ubsan_standalone-x86_64 -Wl,--no-whole-archive -o simpledll.so -fPIC simpledll.cpp
Result:
==11478==Sanitizer CHECK failed: /home/development/llvm/3.9.0/final/llvm.src/projects/compiler-rt/lib/ubsan/ubsan_init.cc:61 ((UBSAN_MODE_UNKNOWN)) != ((ubsan_mode)) (0, 0)
Note that in Step II, if we substitute the function in the shared library with the one that has no undefined behavior code, the program runs without a CHECK failed error. This indicates that UBSAN has found an undefined behavior code, however it was unable to report it properly.
Regards,
Jaszczur

How to link a .a file in C++

I'm trying to get the yaml-cpp parser working on my computer. I followed the instructions on the README, which generated the file libyaml-cpp.a with no errors or warnings. Then I copied that file into a directory, let's call it /path/to/files, where I also put b.yaml, and main.cpp, which contains the following text:
// main.cpp
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
return 0;
}
This comes from the first line of the yaml-cpp tutorial. I tried compiling this while linking to the yaml-cpp library in a few different ways, all of which lead to the same compile-time error: use of undeclared identifier 'YAML'. Here are some of the things I tried:
g++ main.cpp -lyaml-cpp -L/path/to/files
g++ main.cpp libyaml-cpp.a
g++ main.cpp libyaml-cpp.a -lyaml-cpp -L/path/to/files
and so on. How do I compile this correctly or more properly debug this process?
==EDIT==
Now my main.cpp file looks like this:
// main.cpp
#include <iostream>
#include "yaml.h"
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
return 0;
}
Here's my compile command and error message:
$ g++ main.cpp -lyaml-cpp -I/Users/benlindsay/scratch/yaml-cpp/include -L/Users/benlindsay/scratch/yaml-cpp/build
main.cpp:10:3: error: use of undeclared identifier 'YAML'
YAML::Node config = YAML::LoadFile("b.yaml");
^
main.cpp:10:23: error: use of undeclared identifier 'YAML'
YAML::Node config = YAML::LoadFile("b.yaml");
^
2 errors generated.
make: *** [a.out] Error 1
/Users/benlindsay/scratch/yaml-cpp/include contains a yaml-cpp directory, which in turn contains all the .h files including yaml.h. /Users/benlindsay/scratch/yaml-cpp/build contains the lyaml-cpp.a file.
Ok, I downloaded yaml-cpp and tried out, Here is a working version
#include <iostream>
#include "yaml-cpp/yaml.h" //You need to prepend the yaml-cpp
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
//return 0; In cpp, return 0 is not required on main, hence commented
}
The compile using g++ -std=c++11 main.cpp -lyaml-cpp -I/Users/benlindsay/scratch/yaml-cpp/include -L/Users/benlindsay/scratch/yaml-cpp/build

compiling xerces-c++ program with Eclipse

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