0

I am trying to make a custom library to install with CMake. This is a personal utilities library so contains things like an extended math library, extended vectors, file IO functions etc.

I am very new to CMake and I am struggling to build/install this as a library. I would like this to be a single output file which I install. And I would like to be able to include it (and all sub libraries) with

#include <MaxLib.h>

and by adding -LMaxLib to my makefile.

My file structure is like this:

- MaxLib.h
 - CMakeLists.txt
 - File
     - File.cpp
     - File.h
     - CMakeLists.txt
 - Geom
     - File.cpp
     - File.h
     - CMakeLists.txt

I have tried compiling the individual sublibraries i.e:

add_library(File SHARED ${CMAKE_CURRENT_SOURCE_DIR}/File.cpp)
target_include_directories(File)

and I have tried joining them all together with:

add_library(${PROJECT_NAME} STATIC
    ${CMAKE_CURRENT_SOURCE_DIR}/MaxLib.h
)

add_subdirectory(File)
add_subdirectory(Geom)
target_link_libraries(MaxLib File)
target_link_libraries(MaxLib Geom)

And although this compiles and I can install it, the object files for File and Geom are not becoming linked with MaxLib

I get the impression that I'm doing this all wrong... What is the correct way to make a library?

Should I be attempting to make a static or shared library like this?

Should it just be one makefile in the root folder which builds a single static object?

Any advice is welcome! Thanks in advance

Reference to previous question

  • @AnoopRana Why did you close this? – Quimby Jun 05 '22 at 14:30
  • I do not think you want the sublibraries to be shared. That way, they will only be listed as a dependency of `MaxLib` but not included into the static lib itself. Try making them static. – Quimby Jun 05 '22 at 14:31
  • @Quimby I have set the root and sub-libraries to STATIC and I still receive an "undefined reference to `MaxLib::Geom::CleanAngle(double)'" error, I also tried changing the root to SHARED and I got the same issue – Max Peglar-Willis Jun 05 '22 at 14:46
  • Then please create a [mcve] with how you want to consume the library. If you make all libs static, I would assume that `g++ -lMaxLib -L main.cpp` with a simple `main` calling `CleanAngle` should work. – Quimby Jun 05 '22 at 14:49
  • I mean, in your previous question, you got the error because the sublibraries were shared, if they are static, they should get imported into `MaxLib`. What does `nm /usr/local/lib/MaxLib.a` or (wherever it is saved) prints? Does it show all symbols from `Geom` too? – Quimby Jun 05 '22 at 14:53
  • [Here](https://github.com/maxomous/MaxLib) is the library I am trying to write. The minimum reproducible example is just: '#include ' 'int main() { MaxLib::Geom::CleanAngle(7654); }' – Max Peglar-Willis Jun 05 '22 at 15:16
  • @Quimby When i compile this with terminal using 'g++ -std=c++17 -lMaxLib -L/usr/local/lib main.cpp' i get the same 'undefined reference' error – Max Peglar-Willis Jun 05 '22 at 15:17
  • @Quimby 'nm /usr/local/lib/libMaxLib.a' shows nothing – Max Peglar-Willis Jun 05 '22 at 15:20
  • Your library looks simple enough and can be just a static library file (archive). So remove the sub projects (CMake files) and create one library `add_library(MaxLib STATIC ${SOURCES})` where `SOURCES` will have all the cpp files. – ixSci Jun 05 '22 at 16:07
  • @MaxPeglar-Willis Literally nothing? I do not know how that is possible. – Quimby Jun 05 '22 at 16:49
  • CMake provides some support for adding the dependencies automatically, if you use your library in another **C**Make project. Check out the `install` command, `find_package` and CMake's functionality of generating package configuration scripts. This is imho the way to go; it doesn't restrict you to a static lib without dependencies and allows you to use `find_package(MaxLib) ... target_link_libraries(SomeTarget PRIVATE MaxLib)` to automatically add include dirs, linker options, dependencies ect. to the linking target. – fabian Jun 05 '22 at 17:01
  • @Quimby Literally nothing! – Max Peglar-Willis Jun 05 '22 at 19:22
  • @ixSci Yes this seems to work, it just felt wrong, I was told that a makefile should be in each dir? – Max Peglar-Willis Jun 05 '22 at 19:23
  • No it shouldn't. If you feel that it is wrong then you can try to find some solution which won't feel that way. – ixSci Jun 06 '22 at 04:51

0 Answers0