Skip to content

[Rcpp-devel] Using CMakeLists.txt

8 messages · Etienne B. Racine, Dirk Eddelbuettel, Richard Downe

#
I'd like to use a PCL algorithm inside R and they provide a CMakeLists.txt
for configuration. I haven't found how to directly use the CMakeLists.txt
in building R package, however when I run `make .` form src, I can generate
a Makefile  that nearly works with `R CMD INSTALL --no-multiarch`.

It does compile correctly my .cpp, however I get
`Error in library.dynam(lib, package, package.lib) :  shared object
?my_package.so? not found`

Here's the CMakeFileLists.txt :
```
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

find_package(PCL 1.6 REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (segmentation segmentation.cpp)
target_link_libraries (segmentation ${PCL_LIBRARIES})
```
I've seen that using a makefile wasn't the best practice [1] (I should move
it to makevars). Since my CMakeFileLists is apparently simple (e.g.
compared to RInside examples), I guess I could move it to makevars however
I'm not familiar with cmake nor makevars, so I was wondering if there was
documentation about translating from one to the other. However I'm
absolutely not sure this is the real problem (using the CMakeLists.txt).

(I'm using R version 2.15.2 (2012-10-26) on x86_64-pc-linux-gnu (64-bit),
building from RStudio.)

Etienne

[1]:
http://stackoverflow.com/questions/12976036/how-to-use-usedynlib-correctly-in-an-r-package-namespace-file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130305/fb4b2611/attachment.html>
#
On 5 March 2013 at 12:21, Etienne B. Racine wrote:
| I'd like to use a PCL algorithm inside R and they provide a CMakeLists.txt for
| configuration. I haven't found how to directly use the CMakeLists.txt in
| building R package, however when I run `make .` form src, I can generate a
| Makefile? that nearly works with `R CMD INSTALL --no-multiarch`.

Read up a little on CMake. It is at the end of the "just another" frontend /
make replacement.  If you turn on verbose mode, you should be able to see all
g++ invocations.

Which is all you need to mimic in Makevars.

| It does compile correctly my .cpp, however I get
| `Error in library.dynam(lib, package, package.lib) :? shared object
| ?my_package.so? not found`
| 
| Here's the CMakeFileLists.txt :
| ```
| cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
| 
| find_package(PCL 1.6 REQUIRED)
| 
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
| 
| include_directories(${PCL_INCLUDE_DIRS})
| link_directories(${PCL_LIBRARY_DIRS})
| add_definitions(${PCL_DEFINITIONS})
| 
| add_executable (segmentation segmentation.cpp)
| target_link_libraries (segmentation ${PCL_LIBRARIES})
| ```
| I've seen that using a makefile wasn't the best practice [1] (I should move it
| to makevars). Since my CMakeFileLists is apparently simple (e.g. compared to
| RInside examples), I guess I could move it to makevars however I'm not familiar
| with cmake nor makevars, so I was wondering if there was documentation about
| translating from one to the other. However I'm absolutely not sure this is the
| real problem (using the CMakeLists.txt).

Writing Makefile code is yet another little skill one has to acquire on the
side once projects get larger.  There is a lot of support though, as there
are plenty of examples.

Nothing really Rcpp-specific here.

Dirk

| 
| (I'm using R version 2.15.2 (2012-10-26) on x86_64-pc-linux-gnu (64-bit),
| building from RStudio.)
| 
| Etienne
| 
| [1]: http://stackoverflow.com/questions/12976036/
| how-to-use-usedynlib-correctly-in-an-r-package-namespace-file
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
What I've been doing the past couple years, which has worked extremely 
well, is to use a configure script (I've used perl because it allows me 
to do a bunch of other build-time processing that's specific to my 
package, but bash would work fine as well).

All you need (for perl) is:

#!/usr/bin/perl
chdir "src/";
system("cmake .");
chdir "..";
exit(0);

The end result is that R CMD INSTALL [your package] finds an up to date 
makefile in src, but otherwise runs conventionally, and your package 
gets built and installed properly.

-rd
On 03/05/2013 02:09 PM, Dirk Eddelbuettel wrote:
#
On 5 March 2013 at 15:44, Richard Downe wrote:
| What I've been doing the past couple years, which has worked extremely 
| well, is to use a configure script (I've used perl because it allows me 
| to do a bunch of other build-time processing that's specific to my 
| package, but bash would work fine as well).
| 
| All you need (for perl) is:
| 
| #!/usr/bin/perl
| chdir "src/";
| system("cmake .");
| chdir "..";
| exit(0);
| 
| The end result is that R CMD INSTALL [your package] finds an up to date 
| makefile in src, but otherwise runs conventionally, and your package 
| gets built and installed properly.

Etienne was (very correctly) alluding to the fact the _creating your own
src/Makefile is strongly discouraged_ as e.g. repeatedly stated by Simon and
others on r-devel or here.  R's ability produce 32 and 64 bit binaries on the
OSs that support it depends criticially on its ability to write its own
Makefile, based on its stubs and the user's Makevars.

Did you by chance mean Makevars here?  Otherwise the trick is fine but for
CRAN I would expect to be met with, ahem, criticism.

Dirk
#
Also, you're failing to tell CMake to build a shared library.
You need
SET(CMAKE_C++_CREATE_SHARED_LIBRARY 1)

And if you're using Rcpp, also need to explicitly add Rcpp link flags 
(Rscript -e Rcpp:::LdFlags())
-rd
On 03/05/2013 03:44 PM, Richard Downe wrote:
#
I must admit, it's been awhile since I cobbled together my solution; I 
know it's worked seamlessly on 2 or 3 linux variants, but hasn't been 
tested beyond that.

I do recall getting frustrated with making cmake interact with Makevars;

On http://cran.r-project.org/doc/manuals/R-exts.html, a configure script 
seems to be described as an equally respectable alternative to Makevars 
-- it sounds as though Makevars is a facility created to make writing 
extension build processes easier, but for those who have requirements 
that can't be met by Makevars, a configure script is a completely 
acceptable route.  (A plain Makefile I can understand cautioning 
against, because make as a tool is too limited to cover a true range of 
portability concerns).

I do know that CMake is more than capable of covering the bases handled 
by Makevars, and would assume that as long as the correct include and 
linker flags were solicited in CMakeLists.txt things would work correctly.
-rd
On 03/05/2013 03:56 PM, Dirk Eddelbuettel wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130305/b1c210a0/attachment.html>
#
One other detail:
cmake by default names all library outputs "lib[TARGET]". (e.g., there 
*is* no my_package.so, but possibly a libmy_package.so)

If you add the line

SET_TARGET_PROPERTIES(fusionIndices PROPERTIES PREFIX "")

you'll have better luck as well.
-rd
On 03/05/2013 04:00 PM, Richard Downe wrote:
#
On 5 March 2013 at 16:06, Richard Downe wrote:
| On http://cran.r-project.org/doc/manuals/R-exts.html, a configure script seems
| to be described as an equally respectable alternative to Makevars -- it sounds

No. configure determines compile-time _values_ which are then inserted into a
Makevars.in stub file.

May I suggest that we we stop this tangent discussion now?  We are about R
packages here, and there is clearly defined set of tools.

Dirk