Trouble linking a mex file to static mpfr library - c

I am trying to compile a mex file that requires the mpfr C library, using MATLAB R2013a on Mac OS 10.8.5. I would like it to run on systems that don't have a separate installation of MPFR, so I am trying to include the static library:
mex my_program.c libmpfr.a
I copied the libmpfr.a library into the folder with my source code to simplify things. When I run this command I get the following error message:
ld: targeted OS version does not support use of thread local variables in _mpfr_add for architecture x86_64
A little research suggested that this issue could be due to a problem with the llvm-gcc-4.2 compiler that comes with Xcode and is used in MATLAB by default. So, I tried to set up a different compiler. I got GCC 4.7.4 (the latest version supported by MATLAB) from MacPorts, as described here: http://www.ficksworkshop.com/blog/14-coding/65-installing-gcc-on-mac. Next, I edited the mexopts.sh file to point MATLAB to the correct compiler, by entering the following settings:
CC='/opt/local/bin/gcc-mp-4.7'
CXX='/opt/local/bin/g++-mp-4.7'
Now when I try to compile, I get a different error message:
In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294:0,
from /Applications/MATLAB_R2013a.app/extern/include/mex.h:58,
from include/main.h:5,
from include/poisson.h:7,
from src/main.c:22:
/Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:61:21: fatal error: float.h: No such file or directory
If, in mexopts.sh, I additionally set
SDKROOT='/opt/local/'
or a number of other choices (based on the locations of different copies of float.h that I can find on my system), I instead see
my_program.c:10:20: fatal error: stdlib.h: No such file or directory
So it seems that GCC-MP-4.7 is having trouble locating all of the standard C libraries.
I'd be grateful for a solution to either of these issues (linking to static mpfr useing the default compiler, or configuring GCC 4.7 to find the standard C libraries).

I found that the "thread local variables" error was caused by the compiler trying to include backwards-compatibility to OSX 10.5. Since I don't need it to do this, I edited the mexopts.sh file, replacing this line
MACOSX_DEPLOYMENT_TARGET='10.5'
with this:
MACOSX_DEPLOYMENT_TARGET='10.8'
This resulted in the mex file compiling successfully using the built-in Xcode compiler.

Related

Static build of GMP for MSVC (Windows)

Is it possible to build GMP for MSVC on Windows?
I need fully static solution (static library), without any DLL dependencies. So that my final EXE doesn't depend on any external (non-system) DLLs.
I'm alright if building GMP will need Cygwin or MSYS, as far as it can be used later in MSVC without any problems. But as far as I know at least Cygwin builds always depend on extra DLLs like cygwin1.dll which is not affordable for me, fully static-library solution is needed.
I'm aware there exists MPIR library that is more Windows friendly. But right now I need specifically GMP solution if possible.
Of course would be great if all optimizations and assembly is used when building for Windows. But if assembly is not possible then at least non-assembly (generic) variant of GMP is needed.
Of course I need 64-bit version.
If someone can post all steps needed to produce such static library for MSVC usage? Or maybe link some web-site that has such instructions?
I successfully managed to compile a working fully statically linked program with GMP using MSVC under Windows.
For that I used installation of MSYS, which is located in c:/bin/msys/ on my machine.
Then inside MSYS shell installed GMP packages mingw-w64-clang-x86_64-gmp and gmp-devel (pacman -S gmp-devel to install and pacman -Ss gmp to search).
In MSVC compiler I added include directory c:/bin/msys/clang64/include/.
Wrote an example of GMP usage program in C++, that implements Trial Division / Pollard's Rho / Pollard's P-1 factoring algorithms using long arithmetics. This program uses both mpz_...() C routines and mpz_class C++ wrapper class. For example this program is located in main.cpp.
To linker command line I added following libraries:
c:/bin/msys/clang64/lib/libgmp.a
c:/bin/msys/clang64/lib/libgmpxx.a
c:/bin/msys/mingw64/lib/gcc/x86_64-w64-mingw32/10.3.0/libgcc.a
c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a
Also I had to add /FORCE flag (read about it here) to linker command, because libmingwex.a has some symbols overlapping with default MSVC's libraries, precisely without /FORCE I had following errors:
libucrt.lib(strnlen.obj) : error LNK2005: wcsnlen already defined in libmingwex.a(lib64_libmingwex_a-wcsnlen.o)
libucrt.lib(strnlen.obj) : error LNK2005: strnlen already defined in libmingwex.a(lib64_libmingwex_a-strnlen.o)
bin\win-msvc-m-64-release\drafts\gmp_int_msvc.exe : fatal error LNK1169: one or more multiply defined symbols found
All steps produced working (tested) final statically-linked program without any external DLL dependencies (of course except for default system DLLs of Windows).
It means MSYS's libraries .a are fully compatible with MSVC and link successfully in MSVC compilation.
Not to have /FORCE linker flag I also did extra following steps. Made a copy of c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a library. Used c:/bin/msys/clang64/bin/objcopy.exe util, which probably was installed together with Clang. With objcopy renamed overlapping symbols:
objcopy --redefine-sym wcsnlen=msys_wcsnlen libmingwex.a
objcopy --redefine-sym strnlen=msys_strnlen libmingwex.a
which allowed me to successfully use this modified libmingwex.a library to link in MSVC without using /FORCE.

Having issues compiling an exploit using GCC

I was trying to compile a C exploit for a security class I'm in and was struggling to get GCC to perform. The issue is that my /usr/include folder is missing folders that GCC is looking for to handle the includes of the file. The first error below describes a folder that doesn't exist.
asm/page.h: No such file or directory
What I've tried so far:
Symlink it with my /usr/src/linux-headers-4.4.0-kali1-common/include/* folders, but files within that folder start throwing errors that they in turn can't find other files.
Using GCC's -I parameter to manually specify each folder to look in for my includes but this also doesn't work. (Below)
gcc 10613.c -o workdamnit-I/usr/src/linux-headers-4.4.0-kali1-common/include/asm-generic/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/linux/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/asm-generic/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/linux/
ERROR: In file included from /usr/include/stdio.h:33:0,
from 10613.c:2:
/usr/src/linux-headers-4.4.0-kali1-common/include/linux/stddef.h:4:31: fatal error: uapi/linux/stddef.h: No such file or directory
compilation terminated.
I updated the import statement to use page.h from my kali linux common headers. When I tried to run this, I received the below error:
'PAGE_SIZE' undeclared (first use in this function).
Lastly, I tried to compile with wine gcc but this particular exploit uses a socket library that I guess can't be compiled on a windows machine.
GCC version: 5.3.1
Link to exploit: https://www.exploit-db.com/exploits/10613/
My knowledge of C and its compilation requirements is very limited. Any assistance would be greatly appreciated.
Please give a usable and compilable example: https://stackoverflow.com/help/mcve
Based on the errors, it looks like -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/linux/ should actually probably be -I/usr/src/linux-headers-4.4.0-kali1-common/include.

unable to compile C files (MEX) in matlab (Windows) using GNU GCC

I am trying to compile native linux c files on windows using GNU GCC compilers. For the installation purpose, I have followed the steps mentioned by Faheem in the following link :
Using GCC(minGW) as Matlab's MEX compiler
the example given there compiled successfully. but when i am trying to compile a file using thread functions, the following error is being thrown:
fatal error: pthread.h: No such file or directory
#include
^
compilation terminated.
I have installed all the components from MinGW installer related to GNU C compiler bu still the error persists. Can any one suggest a way to solve this problem. Thanks in advance.
According to the TDM-GCC "Quircks" page, TDM-GCC includes a pthreads emulation layer for Microsoft Windows systems, called "winpthreads", and a recent toolchain you should have x86_64-w64-mingw32\include\pthread.h in the TDM install path. So, I don't know what is wrong with Faheem's instructions, but you should have no trouble including pthread.h if MATLAB is configured correctly.
What is not stated on the TDM Quircks page is that TDM uses Winpthreads from the MinGW-w64 project. From the current README installed with TDM:
"Winpthreads" is one of the libraries distributed by the MinGW-w64 project, and
it allows GCC to be compiled with full pthreads compatibility, which is
necessary to enable std::thread and other threading related functions in the
C++ runtime.
As it states, Pthreads is part of the MinGW-w64 project and you can also get it with MinGW-w64 if you pick a pthreads enabled toolchain. For MinGW-w64, I do the following to set it up (see here for more details):
Grab the latest revision for w64 from Sourceforge (or use the installer to choose the toolchain you want, picking a pthread version).
Extract it so that you have a path to the compiler like C:\mingw-w64\x86_64-4.9.2-release-posix-seh-rt_v3-rev1\bin\x86_64-w64-mingw32-g++.exe. There is no need for MSYS, cygwin, or any other environment if you plan on compiling in MATLAB with mex.
Set it up with a custom mexopts.bat or using the new xml configuration system. On my GitHub repo, I have C++ configurations for MinGW-w64 in both file types: mingw_mexopts.bat and mex_C++_mingw-w64.xml. For example, using the xml file to set up C++ MEX file compilation:
mex -setup:C:\Users\Jon\Documents\MATLAB\mex_C++_mingw-w64.xml C++
If needed, set up the C compiler in a similar manner by modifying the config files.
I posted a more detailed version of the above instructions in my answer to the canonical question on the MinGW-with-MATLAB topic.

Statically Linking glib

I'm trying to statically link glib into my C program. I'm not sure what's the best way to do this. I downloaded the code and put it in a subdirectory called glib-2.36.4. I added "-Iglib-2.36.4" when using gcc. The glib.h is in the glib-2.36.4/glib directory and in that file there are references to other header files under the glib directory (such as #include ).
I'm not sure why that is since both glib.h and these other header files are at the same level (in glib subdirectory). I got a compile error due to galloca.h not being found (even though it's there). So I copied glib.h up one level and those errors went away. I then got an error about a missing glibconfig.h. I copied that from my usr directory and that error went away. I compiled my project and now I'm getting an error about undefined reference to g_ptr_array_new. I guess this must be because I haven't actually compiled glib. I had tried to build glib, but when I typed "./configure", but I got this message:
checking if arpa/nameser_compat.h is needed... configure: error: could not compile test program either way
I did install glib using yum, but I really want this code to run even if glib is not installed on a machine.
You need to install both glib and glib-dev via yum, compile using ./configure, (take a look in the ./configure script to see if there are any flags you need to supply or defines you need to produce the static build), without moving any files about, and then you need to compile your code using -i path/to/glib/includes and link with -L path/to/built/static/library

Compiling 64 bit DLL for JNI

I want to include a C library in my Java project via JNI. I wrote the necessary JNI wrapper code and I have compiled and tested it in a Linux environment using gcc and make. Now I need to compile this to make a 64 bit Windows DLL, and I cannot get it to compile.
I downloaded Visual C++ Express 2010 and I have been using cl.exe on the command line. In the absence of knowing any better way to do it, I have just called cl.exe with all of the files I want to compile as arguments. I get a variety of errors:
Command line warning D9024: unrecognized source file type 'svm_jni.h'...
and
svm_jni.c(63) : error C2275: 'jobject' : illegal use of this type as an expression...
The first problem I have discovered is do to the fact that cl.exe does not accept .h files (I guess its only meant for C++ instead of C?). Is there a workaround for this? I could change all of the .h files to .c files and change the include statements, but I would prefer not to do this.
I have tried compiling using make and gcc on MinGW, but it always says that it cannot compile to a 64 bit target.
I have tried doing things through VC++ using the makefile project type, but I could not figure out how that works.
Any suggestions?
EDIT: I removed the .h files from the command line arguments and that solves part of the problem. I have been using
-I "C:\Program Files\Java\jdk1.6.0_21\include" -I "C:\Program Files\Java\jdk1.6.0_21\include\win32"
to get jni.h and jni_md.h. I still get
svm_jni.c(63) : error C2275: 'jobject' : illegal use of this type as an expression
C:\Program Files\Java\jdk1.6.0_21\include\jni.h(83) : see declaration of 'jobject'
and a bunch of syntax and weird errors after that. I am assuming all the errors are the result of a common problem, but I don't know whats going wrong.
Is there a 64 bit version of jni_md.h? The one I'm using now is in \include\win32
You don't really want to compile the header files, rather you want to include them in your compilation path when you compile the c/c++ files.
As for the jobject issue, you need to include the jni header files which are located under the %JAVA_HOME%\include directory.
For Visual C++ Express, did you download the 64 bit building tools? And when you state that gcc and MinGW cannot compile to a 64 bit target, what message are you getting exactly? Do you have minGW-w64?

Resources