Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[doc] improve CMake example in setup your project
[simgrid.git] / doc / doxygen / install_yours.doc
1 /*! @page install_yours Setup your own project
2
3 @tableofcontents
4
5 It is not advised to modify the simgrid source code directly, as it
6 will make it difficult to upgrade to the next version of SimGrid.
7 Instead, you should create your own working directory somewhere on
8 your disk (say `/home/joe/MyFirstScheduler/`), and write your code in
9 there. 
10
11 Then, you should find a solution to get your code compiled and linked
12 to the SimGrid library as needed. This page helps you to do so with
13 several tools: 
14 @ref install_yours_cmake "CMake" and
15 @ref install_yours_makefile "Makefile."
16 If you configure your project with a tool that is not listed here,
17 we'd be glad to hear how you've done that to extend this
18 documentation.
19
20 @section install_yours_cmake Building your project with CMake
21
22 Here is a `CMakeLists.txt` that you can use as a starting point for
23 your project. It builds two simulators from a given set of source files.
24
25 You first need to copy the `FindSimGrid.cmake` (at the root of the
26 SimGrid tree) into the `cmake/Modules` directory of your project.
27
28 @verbatim
29 project(MyFirstScheduler)
30
31 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
32
33 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
34 find_package(SimGrid REQUIRED)
35 include_directories(${SimGrid_INCLUDE_DIR})
36
37 set(SIMULATOR_SOURCES main.c other.c util.c)
38 add_executable(my_simulator ${SIMULATOR_SOURCES})
39 target_link_libraries(my_simulator ${SimGrid_LIBRARY})
40
41 set(OTHER_SOURCES blah.c bar.c foo.h)
42 add_executable(other_xp ${OTHER_SOURCES})
43 target_link_libraries(other_xp ${SimGrid_LIBRARY})
44 @endverbatim
45
46 @section install_yours_makefile Building your project with Makefile
47
48 Here is a Makefile that will work if your project is composed of three
49 C files named @c util.h, @c util.c and @c mysimulator.c. You should
50 take it as a starting point, and adapt it to your code. There is a
51 plenty of documentation and tutorial on Makefile if the file's
52 comments are not enough for you.
53
54 @verbatim
55 # The first rule of a Makefile is the default target. It will be built when make is called with no parameter
56 # Here, we want to build the binary 'mysimulator'
57 all: mysimulator
58
59 # This second rule lists the dependencies of the mysimulator binary
60 # How this dependencies are linked is described in an implicit rule below
61 mysimulator: mysimulator.o util.o
62
63 # These third give the dependencies of the each source file
64 mysimulator.o: mysimulator.c util.h # list every .h that you use
65 util.o: util.c util.h
66
67 # Some configuration
68 SIMGRID_INSTALL_PATH = /opt/simgrid # Where you installed simgrid
69 CC = gcc                            # Your compiler
70 WARNING = -Wshadow -Wcast-align -Waggregate-return -Wmissing-prototypes \
71           -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes \
72           -Wmissing-declarations -Wmissing-noreturn -Wredundant-decls \
73           -Wnested-externs -Wpointer-arith -Wwrite-strings -finline-functions
74
75 # CFLAGS = -g -O0 $(WARNINGS) # Use this line to make debugging easier
76 CFLAGS = -g -O2 $(WARNINGS) # Use this line to get better performance
77
78 # No change should be mandated past that line
79 #############################################
80 # The following are implicit rules, used by default to actually build
81 # the targets for which you listed the dependencies above.
82
83 # The blanks before the $(CC) must be a Tab char, not spaces
84 %: %.o
85         $(CC) -L$(SIMGRID_INSTALL_PATH)/lib/    $(CFLAGS) $^ -lsimgrid -o $@
86 %.o: %.c
87         $(CC) -I$(SIMGRID_INSTALL_PATH)/include $(CFLAGS) -c -o $@ $<
88
89 clean:
90         rm -f *.o *~
91 .PHONY: clean
92 @endverbatim
93
94 @section install_yours_javaexamples Building the Java examples in Eclipse
95
96 If you want to build our Java examples in Eclipse, get the whole
97 source code and open the archive on your disk. In Eclipse, select
98 the menu "File / Import", and then in the wizard "General / Existing
99 Project into Workspace". On the Next page, select the directory
100 "examples/java" that you can find in the SimGrid source tree as a root
101 directory and finish the creation.
102
103 The file \c simgrid.jar must be in the root directory of the SimGrid
104 tree. That's where it is built by default, but if you don't want to
105 compile it yourself, just grab that file from the SimGrid website and
106 copy it in here.
107
108 Please note that once you better understand SimGrid, you should not
109 modify the examples directly but instead create your own project in
110 eclipse. This will make it easier to upgrade to another version of
111 SimGrid.
112
113 @section install_yours_trouble Troubleshooting your project setup
114
115 @subsection install_yours_trouble_libpath error while loading shared libraries: libsimgrid.so
116
117 Sometimes, the following error message (or similar) will be produced:
118 @verbatim
119 ./masterworker1: error while loading shared libraries: libsimgrid.so:
120 cannot open shared object file: No such file or directory
121 @endverbatim
122
123 The same problem can make the execution of your programs spit pages
124 and pages of errors similar to the following. If there is only a few
125 undefined references, then you want to read the next section.
126 @verbatim
127 masterworker.c:209: undefined reference to `sg_version_check'
128 masterworker.c:209: undefined reference to `MSG_init_nocheck'
129 (and many other undefined references)
130 @endverbatim
131
132 In both cases, it means that the system does not manage to find the
133 simgrid library when it tries to execute your programs. Under Linux,
134 specify where to search with the <tt>LD_LIBRARY_PATH</tt> variable.
135 Try running the following command before executing your code. If it
136 helps, you should add this line to your ~/.bashrc so that it gets
137 executed each time you log into your computer.
138
139 @verbatim
140 export LD_LIBRARY_PATH=/opt/simgrid/lib
141 @endverbatim
142
143 @subsection install_yours_trouble_oldlib Only a few undefined references
144
145 Sometimes, the compilation only spits very few "undefined reference"
146 errors. A possible cause is that the system selected an old version of
147 the SimGrid library somewhere on your disk. 
148
149 Under Linux, you can find which version was used with the following
150 command that will display the full path to every used dynamic library.
151 Once you've found the obsolete copy of SimGrid, just erase it, and
152 recompile and relaunch your program.
153 @verbatim ldd yoursimulator
154 @endverbatim
155
156
157 */