help-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setup c/c++ development environment


From: Fredrik Salomonsson
Subject: Re: setup c/c++ development environment
Date: Thu, 04 Mar 2021 11:02:43 -0800

Hi Roy,

Roy Lemmon <roy@roylemmon.com> writes:

> I would like to setup a c/c++ development environment on guix. At the moment, 
> I
> have used gcc-toolchain to bring in the compiler etc. Are other pieces
> necessary ?

That would be the bare minimum for c/c++. I would recommend using a
build system to build your stuff like GNU autotools[0], cmake[1] or
meson[2].

[0] 
https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html
[1] https://cmake.org/cmake/help/latest/
[2] https://mesonbuild.com/Tutorial.html

For the ease of use I would recommend meson.

A cool thing I learned from the Guix days last year was from the talk
"Just build it with Guix" by Efraim Flashner [3]. And that was using
guix to do the testing. It works really well.

[3] 
https://xana.lepiller.eu/guix-days-2020/guix-days-2020-efraim-flashner-build-it-with-guix.mp4

Here is a simplified template of what I use for C++. It uses meson to
build and googletest[4] as the testing framework. But it should be
fairly simple to change to autotools or cmake or another testing
framework, e.g. catch2 [5].

[4] https://github.com/google/googletest/blob/master/docs/primer.md
[5] https://github.com/catchorg/Catch2/blob/devel/docs/why-catch.md#top

-------8<-----------------------------------------------------------------------
(use-modules
  (guix build-system meson)
  (guix gexp)
  (guix git-download)
  (guix packages)
  (guix utils)
  ((guix licenses) #:prefix license:)
  (gnu packages pkg-config)
  (gnu packages check)
  (gnu packages build-tools)
  (gnu packages gcc)
  (ice-9 popen)
  (ice-9 rdelim)
  )

;; From the talk "Just build it with Guix" by Efraim Flashner
;; presented on the Guix days 2020
;; https://guix.gnu.org/en/blog/2020/online-guix-day-announce-2/
(define %source-dir (dirname (current-filename)))

(define %git-commit
  (read-string (open-pipe "git show HEAD | head -1 | cut -d ' ' -f2" 
OPEN_READ)))

(define (skip-git-and-build-directory file stat)
  "Skip the `.git` and `build` directory when collecting the sources."
  (let ((name (basename file)))
    (not (or (string=? name ".git")
             (string=? name "build")))))

(define-public package-name-here
  (package
    (name "package-name-here")
    (version (git-version "0.1.0"  "HEAD" %git-commit))
    (source (local-file %source-dir
                        #:recursive? #t
                        #:select? skip-git-and-build-directory))
    (build-system meson-build-system)
    (arguments
     `(#:meson ,meson-0.55
       ;; Pass flags to meson
       ;; #:configure-flags '("-Dinstall=true")
       ))
    (native-inputs `(("pkg-config" ,pkg-config)
                     ("googletest" ,googletest)
                     ("gcc" ,gcc-9)))
    (synopsis "Template for building with meson.")
    (description "Simple template for building with meson-0.55 and gcc-9. Using
googletest as the testing framework.")
    ;; (home-page "https://...";)
    (license license:gpl3+)
    ))

package-name-here
---------------------------------------------------------------------->8--------

To use it, simply copy the template above into a file called guix.scm,
update the package-name-here and version accordingly and place it at the
root of your project. Note the trailing `package-name-here` at end of
the template, which is there to return a package definition to guix.

Then you can run:

  guix build -f guix.scm

That build your project and run the tests.

If you want to place the guix.scm in a subdirectory, say build-aux.
Change 
  (define %source-dir (dirname (current-filename)))
to
  (define %source-dir (dirname (current-source-directory)))

In the template above.

After that you just run it with

  guix build -f build-aux/guix.scm

I hope that helps.

-- 
s/Fred[re]+i[ck]+/Fredrik/g



reply via email to

[Prev in Thread] Current Thread [Next in Thread]