Skip to content

Commit c2a8363

Browse files
committed
added c-programs example with library+binary
1 parent 74e1e9e commit c2a8363

File tree

8 files changed

+73
-0
lines changed

8 files changed

+73
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# nix-shell workshop
22

33
## normal c programs
4+
5+
enter environment by typing `nix-shell` in ./c-programs afterwards
6+
change directory `cd main` and run `make`
7+
8+
finally run `./main` to see the ultimate answer:
9+
10+
./main
11+
hello world from the #cccamp2015, the answer is
12+
42
13+
14+
415
## go programs
516
## qt programs
617
## perl

c-programs/default.nix

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
with import <nixpkgs> { };
2+
3+
let
4+
mylib = callPackage ./mylib.nix {};
5+
in
6+
7+
stdenv.mkDerivation rec {
8+
name = "myprogram-${version}";
9+
version = "0.0.1";
10+
11+
buildInputs = [ mylib ];
12+
}
13+
14+

c-programs/main/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all :
2+
g++ main.cpp -l mylib -o main
3+
clean :
4+
rm main

c-programs/main/main.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include <mylib.hpp>
3+
4+
int main() {
5+
std::cout << "hello world from the #cccamp2015, the answer is " << std::endl;
6+
std::cout << mylib(23) << std::endl;
7+
}

c-programs/mylib.nix

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{stdenv}:
2+
3+
stdenv.mkDerivation rec {
4+
name = "mylib-1.0.0";
5+
6+
src = ./mylib;
7+
8+
buildInputs = [ ];
9+
10+
installPhase = ''
11+
mkdir -p $out/lib
12+
mkdir -p $out/include
13+
prefix=$out make install
14+
'';
15+
16+
meta = {
17+
description = "mylib";
18+
homepage = "http://www.mylib.com/";
19+
license = "GPL2";
20+
maintainers = with stdenv.lib.maintainers; [qknight];
21+
};
22+
}
23+

c-programs/mylib/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all :
2+
g++ -c mylib.cpp -o mylib.o
3+
ar rvs libmylib.a mylib.o
4+
g++ -shared -o libmylib.so -fPIC mylib.cpp
5+
6+
install :
7+
cp libmylib.a ${prefix}/lib
8+
cp libmylib.so ${prefix}/lib
9+
cp mylib.hpp ${prefix}/include

c-programs/mylib/mylib.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int mylib(int i) {
2+
return i+42-23;
3+
}

c-programs/mylib/mylib.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int mylib(int i);
2+

0 commit comments

Comments
 (0)