2

I'm having trouble trying to link a library that I recently pushed onto Git to my CMake project.

I have named the library octal (GitHub) and am trying to link it to my other library bulletframe (GitHub). Both projects are created by me.

I anticipated that linking the library wouldn't work the first time, so I only started off with a couple of source files in octal. The folder structure of octal is as follows:

.gitignore
CMakeLists.txt
.git/
build/ # generated project files go here, but right now it's empty
src/
    CMakeLists.txt
    octal.h # this is empty
    octal/
        vector/
            vector.h
            vector.cpp

octal/CMakeLists.txt

#...

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_HOME_DIRECTORY}")

add_subdirectory("src")

octal/src/CMakeLists.txt

macro(add_include)
    set(ALL_INCLUDES ${ALL_INCLUDES} ${ARGV})
endmacro(add_include)

add_include("octal.h")
add_include("octal/vector/vector.cpp" "octal/vector/vector.h")

add_library(${PROJECT_NAME} STATIC ${ALL_INCLUDES})

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})

octal/src/vector/vector.h

#pragma once

namespace octal
{
    class Vector
    {
        // ...
        Vector Add(Vector other);
        Vector Subtract(Vector other);
        Vector Multiply(Vector other);
        Vector Multiply(double x);
        Vector Divide(Vector other);
        Vector Divide(double x);
        // ...
    }
}

These configurations are successful in building a liboctal.a file, however I'm not sure if they are linked with bulletframe correctly. There are multiple files that use classes from octal, but this is the first error that it gives after it tries to compile the first source file, entity.cpp:

fatal error: octal/vector/vector.h: No such file or directory
 #include "octal/vector/vector.h"
                                 ^
compilation terminated.
src\CMakeFiles\bulletframe.dir\build.make:57: recipe for target 'src/CMakeFiles/bulletframe.dir/entity.cpp.obj' failed
mingw32-make.exe[3]: *** [src/CMakeFiles/bulletframe.dir/entity.cpp.obj] Error 1
CMakeFiles\Makefile2:121: recipe for target 'src/CMakeFiles/bulletframe.dir/all' failed
mingw32-make.exe[2]: *** [src/CMakeFiles/bulletframe.dir/all] Error 2
CMakeFiles\Makefile2:133: recipe for target 'src/CMakeFiles/bulletframe.dir/rule' failed
mingw32-make.exe[1]: *** [src/CMakeFiles/bulletframe.dir/rule] Error 2
Makefile:130: recipe for target 'bulletframe' failed
mingw32-make.exe: *** [bulletframe] Error 2

bulletframe has a similar folder structure to octal

bulletframe/CMakeLists.txt:

include(ExternalProject)
set(EXPROJ_DIR    "${CMAKE_HOME_DIRECTORY}/external")
set(LIB_DIR       "${CMAKE_HOME_DIRECTORY}/lib")

set(OCTAL_NAME          "octal-git")
set(OCTAL_GIT           "https://github.com/TheOctopod/octal-cpp.git")
set(OCTAL_PREFIX        "${EXPROJ_DIR}/${OCTAL_NAME}")
set(OCTAL_SOURCE_DIR    "${EXPROJ_DIR}/${OCTAL_NAME}")
set(OCTAL_BUILD_DIR     "${EXPROJ_DIR}/${OCTAL_NAME}/build")
set(OCTAL_INSTALL_DIR   "${LIB_DIR}")

ExternalProject_Add(${OCTAL_NAME}
    PREFIX          ${OCTAL_PREFIX}
    TMP_DIR         ${OCTAL_PREFIX}-tmp
    STAMP_DIR       ${OCTAL_PREFIX}-stamp
    # - Download Step ------------------
    GIT_REPOSITORY  ${OCTAL_GIT}
    # - Update Step --------------------
    UPDATE_COMMAND  ""
    # - Configure Step -----------------
    SOURCE_DIR      ${OCTAL_SOURCE_DIR}
    # - Build Step ---------------------
    BINARY_DIR      ${OCTAL_BUILD_DIR}
    # - Install Step ------------------
    INSTALL_COMMAND ""
    CMAKE_ARGS
    "-DCMAKE_BUILD_TYPE=Release"
)

add_library("octal" STATIC IMPORTED)
set_target_properties("octal" PROPERTIES
    # this is where ExternalProject built liboctal.a at
    IMPORTED_LOCATION "${OCTAL_BUILD_DIR}/src"
)

add_subdirectory(src)

bulletframe/src/CMakeLists.txt

macro(add_include)
    set(ALL_INCLUDES ${ALL_INCLUDES} ${ARGV})
endmacro(add_include)

add_include("entity.cpp")

add_library(${PROJECT_NAME} STATIC ${ALL_INCLUDES})

target_link_libraries(${PROJECT_NAME} "octal")

As it stands, my current configurations are unable to build bulletframe with both the Visual Studio 10 2013 and MinGW Makefiles generators.

A couple concerns come to mind as I witnessed the compiler giving similar errors every time:

  • Am I not building the static library (octal) correctly?
  • Am I not linking it to my project (bulletframe) correctly?

1 Answer 1

1

It looks like you're building octal OK, but aren't providing proper paths to its sources (for bulletframe to be able to #include it) nor to its compiled library.

Fixing the includes issue is simple; just add the path to the imported library's INTERFACE_INCLUDE_DIRECTORIES property. However, fixing the library's path is a little trickier.

With MSVC, the full path to the octal lib will be

${OCTAL_SOURCE_DIR}/lib/<build type>/octal.lib

where <build type> is "Debug", "Release", etc. However, with other compilers, the path will be

${OCTAL_SOURCE_DIR}/lib/liboctal.a

To get round this, I'd do something like:

add_library(octal STATIC IMPORTED)
if(MSVC)
  set_target_properties(octal PROPERTIES
      IMPORTED_LOCATION_DEBUG "${OCTAL_SOURCE_DIR}/lib/Debug/octal.lib"
      IMPORTED_LOCATION_RELEASE "${OCTAL_SOURCE_DIR}/lib/Release/octal.lib"
      INTERFACE_INCLUDE_DIRECTORIES "${OCTAL_SOURCE_DIR}/src")
else()
  set_target_properties(octal PROPERTIES
      IMPORTED_LOCATION "${OCTAL_SOURCE_DIR}/lib/liboctal.a"
      INTERFACE_INCLUDE_DIRECTORIES "${OCTAL_SOURCE_DIR}/src")
endif()
5
  • Funny you mentioned the if-statement -- I actually just added a check for MSVC. However, one thing I'm confused about is why I would need to define the source files of octal. Having octal.lib or liboctal.a is not enough? If I could just directly include the sources, why bother compiling a library? Anyways, thanks for answering and I'll try the solution later on.
    – octopod
    Mar 30, 2015 at 1:35
  • Your solution worked -- though I still would appreciate some clarification on the concern stated in the previous comment. Unless you'd rather have me make another question on that?
    – octopod
    Mar 30, 2015 at 1:54
  • 1
    Really all we're wanting to expose is the public headers of octal, not the full list of sources. See for example this recent answer which gives my opinion on how to structure a library's source files. Since octal's public headers are in the same path as the sources, we need to just give that path. A major advantage of having octal as a self-contained library are that it doesn't need to be compiled multiple times even if it's included by multiple targets.
    – Fraser
    Mar 30, 2015 at 8:02
  • is it because the linked library is IMPORTED that I am required to define the include directory explicitly like that using set_target_properties, or is it just common practice? I can't, say, have the include directory already inside octal.lib as target_include_directories would seem to imply in your linked answer?
    – octopod
    Mar 30, 2015 at 8:29
  • Yes - it's because it's imported. The first paragraph of the docs tries to explain the rationale.
    – Fraser
    Mar 30, 2015 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.