Creating a class suitable for use in other programs

I am creating an evolution simulator in C ++, but not as a “real” executable program, but as a class that other programs should #include. This class, called World, has some functions, such as update(), getInfo()etc.

I have two problems here. Firstly, I don’t know how exactly I should compile the class and what files I should provide to the user program (the one that the #includeclass). Obviously, the program should receive the file .hpp, but what else? The class object file World? This would mean that I would need to compile the user program with the syntax g++ World.o user.o -o user, but is there a way to avoid this (mentioned World.oin my compilation commands)? Similarly, I do not need to include iostream.oin the compilation command.

The second problem is that the Worldclass is #includesome other classes, such as Organism, which, in turn, must include the class Blockin order to inherit from it. How can I compile this code to get a single file World.o(if this is the answer to problem 1)?

+5
source share
3 answers

It seems to me that you want to make a library from your class World. A library usually provides a bunch of types or functions or variables that should be associated with / executable or other libraries.

, ( , ). , .

, . , .

  • ,
  • typedefs,
  • ...

World, Organism, , , , . . , .


, . , . cmake. cmake :

# CMakeLists.txt

# add a shared library to build configuration
add_library(my_library_name SHARED my_library_file1.cpp my_library_file2.cpp)

# add an executable to build configuration
add_executable(my_executable_name executable_file1.cpp executable_file2.cpp)

# link the library to the executable
target_link_libraries(my_executable_name my_library_name)

cmake, , CMakeLists.txt, make , make, .


. :

+3

, . , .

+1

You can create a static or shared library. http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

+1
source

All Articles