I am using premake and GNU scientific library in MacOSX to compile a simple example code
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>
int
main (void)
{
double x = 15.0;
double y = gsl_sf_bessel_J0 (x);
printf ("J0(%g) = %.18e/n", x, y);
return 0;
}
To compile this test_gsl.c from the terminal I can do
clang -Wall -I/usr/local/include -c test_gsl.c
clang -L/usr/local/lib test_gsl.o -lgsl -lgslcblas -lm
I cannot figure out how to write the premake5.lua file.
Related
I have a cpp file that defines c++ and R functions, which are sourced into R using Rcpp::sourceCpp().
When I source the file, the R code is (partially) printed as well, even when I specify showOutput = FALSE (I guess it only applies to cpp code?!).
The question now is: how can I suppress the partial R output without using capture.output() or similar tricks.
MWE
in tester.cpp
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector timesTwo(Rcpp::NumericVector x) {
return x * 2;
}
/*** R
foo <- function(x) timesTwo(x)
*/
When sourcing the file, I see the following:
Rcpp::sourceCpp("tester.cpp", showOutput = FALSE)
#> foo <- function(x) timesTwo(x)
Even shorter MWE
Rcpp::sourceCpp(code='
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector timesTwo(Rcpp::NumericVector x) {
return x * 2;
}
/*** R
foo <- function(x) timesTwo(x)
*/
')
Could this question be rooted in a misunderstanding of what showOutput is for?
Looking at help(sourceCpp) we see
showOutput: ‘TRUE’ to print ‘R CMD SHLIB’ output to the console.
that it affects the actual compilation step and has nothing to do with any R code added as optional bit to also run if present.
The following example should make this clear (and reveal a few of CXX and other settings):
> cppFunction("int doubleMe(int i) { return i+i; }", showOutput=TRUE)
/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_10.so' 'file99f11710553a7.cpp'
ccache g++ -I"/usr/share/R/include" -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/tmp/RtmpC7dZ23/sourceCpp-x86_64-pc-linux-gnu-1.0.5.4" -fpic -g -O3 -Wall -pipe -pedantic -c file99f11710553a7.cpp -o file99f11710553a7.o
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o sourceCpp_10.so file99f11710553a7.o -L/usr/lib/R/lib -lR
>
> cppFunction("int trippleMe(int i) { return i+i+i; }", showOutput=FALSE)
>
Now, suppressing output of R code is a different topic and orthogonal to whether such code contains element made via Rcpp or not.
And lastly #MrFlick is spot on that if you don't want R code sourced as part of a sourceCpp() call ... then just don't include such code! Or just break the regexp /*** R.
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
I'm using the excellent Rcpp project to implement some faster text processing in R, but cannot get the package to build correctly under Windows. (It builds fine under OS X and Linux.) I am using Rcpp, and the BH header packages. I can get the example at http://gallery.rcpp.org/articles/boost-regular-expressions/ and I can get the code below to build on every platform except Windows.
To isolate my problem I removed it from my larger package, and put it into a simple package so that it can be installed using devtools::install_github("kbenoit/boostTest").
The files are:
Makevars.win:
PKG_LIBS = -lboost_regex
src/clean.cpp:
#include <Rcpp.h>
#include <string>
#include <boost/regex.hpp>
// [[Rcpp::depends(BH)]]
const boost::regex re_digits("[[:digit:]]");
const boost::regex re_punct("[[:punct:]]");
const std::string space0("");
std::string removeDigits(const std::string& s) {
return boost::regex_replace(s, re_digits, space0, boost::match_default | boost::format_sed);
}
std::string removePunct(const std::string& s) {
return boost::regex_replace(s, re_punct, space0, boost::match_default | boost::format_sed);
}
// [[Rcpp::export]]
Rcpp::DataFrame cleanCpp(std::vector<std::string> str) {
int n = str.size();
for (int i=0; i<n; i++) {
str[i] = removeDigits(str[i]);
str[i] = removePunct(str[i]);
}
return Rcpp::DataFrame::create (Rcpp::Named("text") = str);
}
and cleanC.R is exported as:
cleanC <- function(x) as.character(cleanCpp(x)[, 1])
(I did this because I am so new to Rcpp that I could not figure out how to return a CharacterVector, but could get the Rcpp::DataFrame return type working by following the boost-regular-expressions example linked above.
In my DESCRIPTION file I have:
LinkingTo: Rcpp,BH
The problem is that when I build the package using devtools::build_win(), it fails, even though it builds fine on Linux and OS X. The output can be seen here:
* installing *source* package 'boostTest' ...
** libs
*** arch - i386
g++ -I"D:/RCompile/recent/R-3.1.3/include" -I"d:/RCompile/CRANpkg/lib/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.1/RcppArmadillo/include" -I"d:/RCompile/CRANpkg/lib/3.1/BH/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -I"D:/RCompile/recent/R-3.1.3/include" -I"d:/RCompile/CRANpkg/lib/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.1/RcppArmadillo/include" -I"d:/RCompile/CRANpkg/lib/3.1/BH/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c clean.cpp -o clean.o
g++ -shared -s -static-libgcc -o boostTest.dll tmp.def RcppExports.o clean.o -lboost_regex -Ld:/RCompile/r-compiling/local/local320/lib/i386 -Ld:/RCompile/r-compiling/local/local320/lib -LD:/RCompile/recent/R-3.1.3/bin/i386 -lR
d:/compiler/gcc-4.6.3/bin/../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lboost_regex
collect2: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'boostTest'
* removing 'd:/RCompile/CRANguest/R-release/lib/boostTest'
Any help appreciated!
I am using OpenCV2 on Ubuntu 12.04. I can successfully run image read-display codes.
However I am not able to run codes with inbuilt functions eg. cvtColor()
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread("img.jpg");
if( image.data == NULL )
{
printf( "file cannot be loaded\n");
return 1;
}
cv::namedWindow("My");
cv::imshow("My", image);
cv::Mat result;
cv::cvtColor(image, result, CV_BGR2Luv);
cv::imwrite("outImg.jpg", result);
cv::waitKey(0);
return 0;
}
I am using Qt-creator for my OpenCV
After compiling with --libs, --cflags I get following compiler error:
make: Entering directory `/home/swaroop/Work/ai-junkies/cuda/uc_davis/opencv2.x/OpenCV2Test'
g++ -g -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I/usr/include/opencv -I. -o main.o main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:22:29: error: 'CV_BGR2Luv' was not declared in this scope
main.cpp:22:39: error: 'cvtColor' was not declared in this scope
Please help me fix this.
cvtColor declared in opencv2/imgproc/imgproc.hpp
keep in mind it's #include not #import
#include <opencv2/imgproc/imgproc.hpp>
Alternatively, if you are testing things and not concerned with overdoing the includes, you can simply have one line:
#include <opencv2/opencv.hpp>
and it will include most opencv2 headers.
Greetings Everyone.
I'm currently trying to compile a multiple-language program (C, C++ and FORTRAN) using GNU compilers in UNIX (g++, gcc & f77 respectively).
All my sources are compiling into objects with no errors however I encounter a symbol referencing error as they are linked as shown below:
f77 -L/usr/sfw/lib -R/usr/sfw/lib -lgcc_s -o SlowDynamic.exe main.o \
SA.o mersenne.o CFE.o MA_57.o blas.o MA_57_Depend.o Metis.o\
BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o
NOTICE: Invoking /usr/bin/f90 -f77 -ftrap=%none -L/usr/sfw/lib -R/usr/sfw/lib -lgcc_s -o SlowDynamic.exe main.o SA.o mersenne.o CFE.o MA_57.o blas.o MA_57_Depend.o Metis.o BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o
Undefined first referenced
symbol in file
_Znwj SA.o
_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev SA.o
_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev SA.o
_ZNKSsixEj main.o
_ZNSolsEPFRSoS_E SA.o
_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode SA.o
_ZNSolsEd SA.o
_ZNSolsEi SA.o
__cxa_end_catch SA.o
__cxa_begin_catch SA.o
_ZdlPv SA.o
_ZNSt14basic_ofstreamIcSt11char_traitsIcEE7is_openEv SA.o
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c SA.o
_ZSt4cerr SA.o
_ZSt4cout SA.o
_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv SA.o
_ZNSt8ios_base4InitD1Ev main.o
_ZNSt8ios_base4InitC1Ev main.o
_ZNKSt9basic_iosIcSt11char_traitsIcEEntEv SA.o
__gxx_personality_v0 main.o
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc SA.o
__cxa_rethrow SA.o
_ZNKSs4sizeEv main.o
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ SA.o
ld: fatal: Symbol referencing errors. No output written to SlowDynamic.exe
*** Error code 1
make: Fatal error: Command failed for target `SlowDynamic.exe'
With the Following Makefile:
products: SlowDynamic.exe
SlowDynamic.exe: main.o SA.o mersenne.o CFE.o BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o MA_57.o blas.o MA_57_Depend.o Metis.o
f77 -L/usr/sfw/lib -R/usr/sfw/lib -lgcc_s -o SlowDynamic.exe main.o \
SA.o mersenne.o CFE.o MA_57.o blas.o MA_57_Depend.o Metis.o\
BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o
main.o: main.cpp
g++ -c -o main.o main.cpp
SA.o: SA.cpp
g++ -c -o SA.o SA.cpp
mersenne.o: mersenne.cpp
g++ -c -o mersenne.o mersenne.cpp
CFE.o: CFE.c
gcc -c -o CFE.o CFE.c
MA_57.o: MA_57.f
f77 -c -o MA_57.o MA_57.f
blas.o: blas.f
f77 -c -o blas.o blas.f
MA_57_Depend.o: MA_57_Depend.f
f77 -c -o MA_57_Depend.o MA_57_Depend.f
Metis.o: Metis.f
f77 -c -o Metis.o Metis.f
BCs.o: BCs.c
gcc -c -o BCs.o BCs.c
EMatrix.o: EMatrix.c
gcc -c -o EMatrix.o EMatrix.c
Numbering.o: Numbering.c
gcc -c -o Numbering.o Numbering.c
KMatrix.o: KMatrix.c
gcc -c -o KMatrix.o KMatrix.c
Solve.o : Solve.c
gcc -c -o Solve.o Solve.c
clean:
rm *.o Main.exe *.gpi
I have read that this is typically the fault of missing libraries. I know the C & FORTRAN code compiles fine seperatly (respective libraries included) as does the C++ code when compiled on its own. This leads me to believe that it is the interface between the two programs that causes the error. Unfortunatly I've little to no experiance debugging this sort of problem and without any clues from the linker its hard to move forward. I'll include the necessary parts of my program that deal with the interface between the two sides of the program.
First the C++ part: SA.h, SA.cpp
SA.h:
class SimAnneal {
...
std::vector<float> DensityArray;
std::vector<float> EnergyArray;
public
double ObjFunction ();
...
}
SA.ccp:
#include <math.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
#include "SA.h"
#include "CFE.h"
#include "randomc.h" //Includes mersenne.cpp
double SimAnneal::ObjFunction ()
{
CFE(&DensityArray[0], &EnergyArray[0]);
// sends pointers of both arrays to CFE.c and modifies EnergyArray as
// shown in CFE.c
double SumStrainEnergy = 0;
for (int i = 0; i < EnergyArray.size(); i++)
{
SumStrainEnergy += EnergyArray[i]; //Effectively sum of array
//engy[] from CFE.c
}
return SumStrainEnergy;
}
Secondly the C/FORTRAN part: CFE.h, CFE.c
CFE.h:
#ifdef __cplusplus
extern "C" {
#endif
void CFE(float density[], float energy[]);
#ifdef __cplusplus
}
#endif
CFE.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BCs.h"
#include "EMatrix.h"
#include "Numbering.h"
#include "KMatrix.h"
#include "fg_types.h"
#include "Solve.h"
void CFE(float density[], float energy[])
{
...stuff...
float * dens;
dens = density; //pass pointer of array density[0] in SA.cpp to CFE.c
...more stuff....
float * engy;
engy = energy; //pass pointer of array energy[0] in SA.cpp to CFE.c
***Modify engy in some respects****
}
Essentially the ObjFunction in SA.cpp is called in main.cpp, which contains main().
Is there any visible fault?
Is there any way I can ask the linker to announce what causes / where the error occours in the linking?
Any help will be much appriciated.
Thank you.
+++ EDIT: Verbose feedback +++
birch $ g++ -v Hello.cpp
Reading specs from /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/specs
Configured with: /sfw10/builds/build/sfw10-patch/usr/src/cmd/gcc/gcc-3.4.3/configure --prefix=/usr/sfw --with-as=/usr/ccs/bin/as --without-gnu-as --with-ld=/usr/ccs/bin/ld --without-gnu-ld --enable-languages=c,c++ --enable-shared
Thread model: posix
gcc version 3.4.3 (csl-sol210-3_4-branch+sol_rpath)
/usr/sfw/libexec/gcc/sparc-sun-solaris2.10/3.4.3/cc1plus -quiet -v Hello.cpp -quiet -dumpbase Hello.cpp -mcpu=v7 -auxbase Hello -version -o /var/tmp//cc2JwHRb.s
ignoring nonexistent directory "/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../sparc-sun-solaris2.10/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/sparc-sun-solaris2.10
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/backward
/usr/local/include
/usr/sfw/include
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/include
/usr/include
End of search list.
GNU C++ version 3.4.3 (csl-sol210-3_4-branch+sol_rpath) (sparc-sun-solaris2.10)
compiled by GNU C version 3.4.3 (csl-sol210-3_4-branch+sol_rpath).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Hello.cpp:6:2: warning: no newline at end of file
/usr/ccs/bin/as -V -Qy -s -xarch=v8 -o /var/tmp//ccvAshv7.o /var/tmp//cc2JwHRb.s
/usr/ccs/bin/as: Sun Compiler Common 10 Patch 09/04/2007
/usr/sfw/libexec/gcc/sparc-sun-solaris2.10/3.4.3/collect2 -V -R/usr/sfw/lib -Y P,/usr/ccs/lib:/usr/lib -Qy /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crt1.o /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crti.o /usr/ccs/lib/values-Xa.o /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crtbegin.o -L/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3 -L/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../sparc-sun-solaris2.10/lib -L/usr/ccs/lib -L/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../.. /var/tmp//ccvAshv7.o -lstdc++ -lm -R/usr/sfw/lib -lgcc_s -lgcc -lc -R/usr/sfw/lib -lgcc_s -lgcc -lc /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crtend.o /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crtn.o
ld: Software Generation Utilities - Solaris Link Editors: 5.10-1.490
Some of those undefined symbols clearly relate to standard C++ classes (i.e. ostream).
You need to ensure that you've got at least the C++ library linked (-lstdc++).
To debug further:
Run f77, g++, etc, in verbose mode (-v) with single language programs and see which libraries the compiler automatically includes in the link phase for each language
For symbols that are in your own code, use nm to look at both the native language declarations and non-native invocations to determine how they mismatch. There are rules about underscore prefixes, etc, which are commonly applied.
(As far as I can remember, but it's 15 years since I last linked C and Fortran, C symbols are always internally prefixed with an underscore, but Fortran symbols are not).
The main problem is that you're doing the linking step with the F77 compiler, which doesn't link in the C++ standard library by default. Like Alnitak said, specify -lg++ explicitly during linking to get it.
Also, if you are calling C++ functions from C code (or from F77 code), make sure you enclose the prototypes of those functions in an extern "C" {} block so that their symbol names show up in the standardized C form, rather than the mangled C++ form (this restricts you from some things, though, such as overloading). See here for more information.
Usually, you should link a program containing any C++ code with the C++ compiler, adding the Fortran and C libraries to the link line. Further, the standard advice is to make the main() program C++, nor Fortran, since the startup sequences for C++ are different. Make the Fortran code into a function that you call from a minimal C++ main.
int main(void) // Assuming no argument handling - probably incorrect
{
return(fortran_main_program());
}