How to include nmath.h? - r

I would like to include the header nmath.h for my C code (within an R package) to find R_FINITE and ML_ERR_return_NAN. I found that one cannot include nmath.h directly. For R_FINITE to be found, I could include R_ext/libextern.h. But I don't know what to include so that ML_ERR_return_NAN is found. Any ideas? I found here that Prof. Brian Ripley referred to Writing R Extensions, but I couldn't find nmath.h being addressed there (where exactly?)

On Debian or Ubuntu:
sudo apt-get install r-mathlib
after which you can build test programs such as this:
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;
// compile-command: "gcc -s -Wall -O3 \
// -I/usr/share/R/include -o rmath_rnorm \
// rmath_rnorm.c -lRmath -lm" -*-
// Compare to
// $ Rscript -e "RNGkind('Marsaglia'); \
// .Random.seed[2:3] <- c(123L, 456L); rnorm(2)"
// [1] -0.2934974 -0.3343770
#include <stdio.h>
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
int main(void) {
set_seed(123, 456);
printf("rnorm: %f %f\n", rnorm(0.0, 1.0), rnorm(0.0, 1.0));
return 0;
}
Note: The first four lines should be one-line in the file you safe, then M-x compile build the program for your. Ditto for the Rscript invocation: one line.
Edit: Drats. Answered the wrong question :) nmath.h appears to not be exported from src/nmath/nmath.h but this R Mathlibrary is what is exported by R Core for use by others. Where as the nmath.h file has
/* Private header file for use during compilation of Mathlib */
#ifndef MATHLIB_PRIVATE_H
#define MATHLIB_PRIVATE_H
so you are not supposed to rely on it.

Related

Accessing an external variable from a C library

I am currently learning C and am trying to understand the possibilities of dynamic libraries.
My current question is, if I have a simple "Hello World" application in C called "ProgA", and this program dynamically loads a shared library with some example code called "LibB", can LibB access a global variable in ProgA, which was declared as external?
Given is the following example code for demonstration of the problem:
file header.h
#ifndef TEST_H
#define TEST_H
typedef struct test_import_s {
int some_field;
} test_import_t;
extern test_import_t newtestimport;
#endif
file prog_a.c
#include <stdio.h>
#include <windows.h>
#include "header.h"
test_import_t newtestimport = {
.some_field = 42
};
int main()
{
HINSTANCE hinstLib;
typedef void (*FunctionPointer)();
newtestimport.some_field = 42;
hinstLib = LoadLibrary("lib_b.dll");
if (hinstLib != NULL)
{
FunctionPointer initialize_lib_b;
initialize_lib_b = (FunctionPointer)GetProcAddress(hinstLib, "initialize_lib_b");
if (initialize_lib_b != NULL)
{
initialize_lib_b();
}
FreeLibrary(hinstLib);
}
return 0;
}
file lib_b.c
#include <stdio.h>
#include "header.h"
test_import_t *timp;
void initialize_lib_b() {
timp = &newtestimport;
int some_field = timp->some_field;
printf("Result from function: %d\n", some_field);
}
file CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(dynamic-library-2 C)
set(CMAKE_C_STANDARD 23)
add_library(lib_b SHARED lib_b.c)
set_target_properties(lib_b PROPERTIES PREFIX "" OUTPUT_NAME "lib_b")
add_executable(prog_a prog_a.c)
target_link_libraries(prog_a lib_b)
In the above example, the headerfile header.h defines the struct test_import_t and an external variable newtestimport using this struct. In the C file of the main program prog_a.c one property of this struct is assigned the value 42. It then dynamically loads the library lib_b.c using the Windows API and executes a function in it. The function then should access the variable newtestimport of the main program and print out the value of the variable (42).
This example does not work. The compiler throws the following error:
====================[ Build | prog_a | Debug ]==================================
C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\cmake\win\x64\bin\cmake.exe --build C:\Users\user1\projects\learning-c\cmake-build-debug --target prog_a -j 9
[1/2] Linking C shared library dynamic-library-2\lib_b.dll
FAILED: dynamic-library-2/lib_b.dll dynamic-library-2/liblib_b.dll.a
cmd.exe /C "cd . && C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\mingw\bin\gcc.exe -fPIC -g -Wl,--export-all-symbols -shared -o dynamic-library-2\lib_b.dll -Wl,--out-implib,dynamic-library-2\liblib_b.dll.a -Wl,--major-image-version,0,--minor-image-version,0 dynamic-library-2/CMakeFiles/lib_b.dir/lib_b.c.obj -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Users\user1\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\223.8617.54\bin\mingw\bin/ld.exe: dynamic-library-2/CMakeFiles/lib_b.dir/lib_b.c.obj:lib_b.c:(.rdata$.refptr.newtestimport[.refptr.newtestimport]+0x0): undefined reference to `newtestimport'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
How can the example be fixed to accomplish the described goal?
Windows DLLs are self-contained, and can not have undefined references similar to newtestimport, unless these references are satisfied by another DLL.
How can the example be fixed to accomplish the described goal?
The best fix is to pass the address of newtestimport into the function that needs it (initialize_lib_b() here).
If for some reason you can't do that, your next best option is to define the newtestimport as a dllexport variable in another DLL, e.g. lib_c.dll.
Then both the main executable and lib_b.dll would be linked against lib_c.lib, and would both use that variable from lib_c.dll.
P.S. Global variables are a "code smell" and a significant source of bugs. You should avoid them whenever possible, and in your example there doesn't seem to be any good reason to use them.

using R's c code in my standalone executables: "Undefined symbols"

Suppose I have the following c source code, in dexp_test.c:
#include <stdio.h>
double dexp(double x, double scale, int log);
int main() {
double x;
x = dexp(1 , 2, 0);
printf("Value: %f\n", x);
return 0;
}
where dexp is defined in R's source code (https://github.com/wch/r-source/blob/trunk/src/nmath/dexp.c). I would like to compile this to a standalone executable. I have R 4.0 installed on my system. I have the following gcc lines:
gcc r-source/src/nmath/dexp.c -I/Library/Frameworks/R.framework/Versions/4.0/Resources/include -c -o a.o
gcc dexp_test.c -c -o b.o
These lines run just fine on my system and I am left with new files a.o and b.o without errors.
When I run this line to get an executable:
gcc -o test_exp a.o b.o
...I get these errors:
Undefined symbols for architecture x86_64:
"_R_NaN", referenced from:
_Rf_dexp in a.o
"_R_NegInf", referenced from:
_Rf_dexp in a.o
"_dexp", referenced from:
_main in b.o
(maybe you meant: _Rf_dexp)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm definitely missing something conceptually here; how do I get this to compile? If it helps, I'm on OSX 15.6, and the output of gcc -v is
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
You should mention the operating system you are using.
You need to include the appropriate headers. And tell the linker where and which libraries you want to use.
So your source should be
#include <stdio.h>
#include <R.h>
#include <Rmath.h>
int main() {
double x;
x = dexp(1 , 2, 0);
printf("Value: %f\n", x);
return 0;
}
And on the command line you should use the following
gcc -I/Library/Frameworks/R.framework/Versions/4.0/Resources/include -L/Library/Frameworks/R.framework/Versions/4.0/Resources/lib -lR -o test_exp trydexp.c

MarbleWidget with Qt5 support - example code no longer exits properly

I'm writing a marine navigation app. I am a Qt5 and Marble newbie, but this was working so well, until I hit this block.
It uses MarbleWidget as a way of displaying outputs from the application, closely modelled on an example application supplied with Marble.
When I ported this to Qt5 at recent releases (opensuse), 5.14.0, it stopped working properly - it will no longer exit the application by clicking on the x button in the top right of the title bar.
This behaviour is exhibited by the sample code supplied with Marble as well.
Here is the sample application:
#include <QtWidgets/QApplication>
#include <marble/MarbleWidget.h>
#include <marble/GeoPainter.h>
using namespace Marble;
class MyMarbleWidget : public MarbleWidget
{
public:
virtual void customPaint(GeoPainter* painter);
};
void MyMarbleWidget::customPaint(GeoPainter* painter)
{
GeoDataCoordinates home(8.4, 49.0, 0.0, GeoDataCoordinates::Degree);
painter->setPen(Qt::green);
painter->drawEllipse(home, 7, 7);
painter->setPen(Qt::black);
painter->drawText(home, "Hello Marble!");
}
int main(int argc, char** argv)
{
QApplication app(argc,argv);
MyMarbleWidget *mapWidget = new MyMarbleWidget;
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
mapWidget->show();
return app.exec();
}
and my Makefile is:
all: pre bld post
bld: quick_marble.x
quick_marble.x : quick_marble.cpp
g++ -g -fpic -I /usr/include/qt5/ -I/usr/include/qt5/QtWidgets \
-I/usr/include/qt5/QtGui \
-I/usr/include/qt5/QtCore \
-o quick_marble.x \
quick_marble.cpp \
-L/usr/local/lib64 -lmarblewidget-qt5 -lQt5Core -lQt5Widgets -lQt5Gui
pre :
rm -f *.x
rm -f *.list
post :
ldd quick_marble.x >quick_marble.modules.list
echo all done
I have tried some work-arounds, but nothing seems to solve this issue.
I have posted the issue in a marble forum, but it hasn't been looked at for several months.
Any ideas to move forward would be helpful.

QBS: qbs fails to run moc when class is declared through a macro in another file

In my project I need to create QObject derived classes (with Q_OBJECT and signals) using a macro. The macro resides in a separate file. Here is a simplified example:
The macro is declared in the file CreateQtClass.h:
#ifndef __CREATEQTCLASS_H__
#define __CREATEQTCLASS_H__
#define CREATE_QT_CLASS( ClassName ) \
class ClassName : public QObject \
{ \
Q_OBJECT \
\
signals: \
Q_SIGNAL void mySignal( void ); \
};
#endif //__CREATEQTCLASS_H__
I use the macro to create my class in the file MyQtClass.h
#ifndef __MYQTCLASS_H__
#define __MYQTCLASS_H__
#include <QObject>
#include "CreateQtClass.h"
CREATE_QT_CLASS( MyQtClass );
#endif //__MYQTCLASS_H__
In my .qbs file, I add MyQtClass.h to the files property, like this:
import qbs
QtApplication {
name: "HelloWorld-Qt"
files: [ "main.cpp", "MyQtClass.h" ]
}
Now, when running qbs build, qbs doesn't run 'moc' on MyQtClass.h. It looks like it doesn't do the scanning correctly, and doesn't detect the Q_OBJECT inside the macro.
(I may note that if the macro declaration and usage are in the same file, everything works fine).
My question:
Is there a way for the user to manually force qbs to run 'moc' on a file?
Maybe we need something like a "force_moc" fileTag (the opposite of "unmocable") which I can apply to a group containing MyQtClass.h.
Addition:
I am adding a simple Makefile and main.cpp to demonstrate that moc works well with the above approach:
The file main.cpp:
#include <QDebug>
#include "MyQtClass.h"
static void mySlot( void )
{
qDebug() << "Hello slot";
}
int main( void )
{
MyQtClass c;
QObject::connect( &c, &MyQtClass::mySignal, &mySlot );
emit c.mySignal();
return 0;
}
The Makefile:
CXX = /usr/bin/g++
MOC = /home/user/programs/Qt5.11.2/5.11.2/gcc_64/bin/moc
INCLUDES = \
-I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/include \
-I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/include/QtCore \
-I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/mkspecs/linux-g++ \
-I/usr/include
LINK_FLAGS = \
-Wl,-m,elf_x86_64,-rpath,/home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib \
-L/home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib \
-m64 /home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib/libQt5Core.so.5.11.2 \
-lpthread
C_FLAGS = \
-g \
-O0 \
-Wall \
-Wextra \
-m64 \
-pipe \
-fexceptions \
-fvisibility=default \
-fPIC \
-DQT_CORE_LIB \
$(INCLUDES) \
-std=c++11
SOURCES = main.cpp
OBJS = $(SOURCES:%.cpp=%.cpp.o)
HEADERS_THAT_NEED_MOC = MyQtClass.h
MOC_OBJS = $(HEADERS_THAT_NEED_MOC:%.h=moc_%.cpp.o)
all: HelloWorld-Qt
HelloWorld-Qt: $(OBJS) $(MOC_OBJS)
$(CXX) $^ $(LINK_FLAGS) -o $#
%.cpp.o : %.cpp
$(CXX) $(C_FLAGS) -c $^ -o $#
moc_%.cpp: %.h
$(MOC) -DQT_CORE_LIB $(INCLUDES) $^ -o $#
clean:
rm -f *.cpp.o HelloWorld-Qt moc_*.cpp
I don't think your approach can work, independent of the build tool you use. Keep in mind that moc looks for the Q_OBJECT macro. No such macro can ever be found in MyQtClass.h, because neither moc nor the build tool supporting it will expand the CREATE_QT_CLASS macro, because macro expansion would also expand away Q_OBJECT.
Note that if you add CreateQtClass.h to your qbs file, you will notice that qbs does run moc -- but on the CreateQtClass file. That's the correct behavior, because that's where the Q_OBJECT macro occurs.
I double-checked with qmake and cmake, and they all behave the same way: If you do not list CreateQtClass in the project file, they will not run moc. If you do list it, moc is run on that file.
If you want to keep using your macro, you'll have to make sure to reference Q_OBJECT at the calling site, e.g. as a macro parameter. But even then, I'm not sure moc itself will be fond of that construct.

Which shell will execute the cmd when popen is called

First of all, forgive my awful english.....
This is the prototype
FILE *popen(const char* cmd_string, const char* type);
Here is my question, the book says that when popen function is called, it will call exec to get a shell to execute the cmd_string we give to popen, but I'm not sure which shell will exec get, so can anyone give me an answer?
/bin/sh : From the doc:
The command argument is a pointer to a null-terminated string
containing a shell command line. This command is passed to /bin/sh
using the -c flag; interpretation, if any, is performed by the shell.
Let's try and see:
$ cat test.c
#include <stdio.h>
int main() {
FILE *fp;
char var[5];
fp = popen("echo $0", "r");
fgets(var, 5, fp);
printf("%s", var);
}
$ gcc -Wall test.c
$ ./a.out
sh

Resources