CMake does not find correct header / include files in include_directories

Again I get an "undefined for x86_64 architecture" error when I try to compile. I've tried more than I can document in this post (because I've forgotten everything I've tried). It's a really simple setup and compiles very easily with CMake ...

When I run make it works fine. But I want to convert it to CMake for interoperability. As you can see, I threw my "$ HEADERS" variable in several places, I tried quite a few places, but I keep getting my error. Depending on where I put this $ {HEADER}, it might also technically generate the error "error: cannot specify -o when generating multiple output files" (this applies if only target_link_library is specified in the declaration ).

I have 2 folders:

Root
    Headers (contains all .h files)
    Source (contains all .cc/.cpp/.c files) (and also a CMakeLists.txt)
CMakeLists.txt

      

My root CMakeLists.txt contains the following:

cmake_minimum_required(VERSION 2.8.4)
project(Framework)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_compile_options("-v")

add_subdirectory(Source)

#Variables for making my life easier and adding the headers
set(H Headers)
include_directories(${H})
set(S Source)
file(GLOB HEADERS
#Add any file in the headers dir
"${H}/*"
)

# Create a variable to use for main.cc
set(MAIN ${S}/main.cc ${HEADERS})

# Add the main.cc file and headers
add_executable(Framework ${MAIN} ${HEADERS})

# Add the .cc/.cpp files
target_link_libraries(Framework ${SOURCE_FILES})

      

My CMakeLists.txt in my source directory contains the following:

file(GLOB SOURCES
"*.cc"
"*.cpp"
)

add_library(SOURCE_FILES ${SOURCES})

      

I don't have any titles, as I assume the documentation states we don't need.

Thanks for the help. I watched:

+3


source to share


1 answer


The main problem here is that you are referring to the target SOURCE_FILES

as a variable. Remove the dollar sign and curly braces.

target_link_libraries(Framework SOURCE_FILES)

      

It also seems strange what you set include_directories

after the call add_subdirectory

, I would be surprised if it worked.

Overall, I think you are making things more complex than they should be. The following should be all you need.

Top level CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Framework CXX)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic")

include_directories(
  ${PROJECT_SOURCE_DIR}/Headers
)

add_subdirectory(Source)

      

Source / CMakeLists.txt

# Do not use file globing because then CMake is not able to tell whether a file
# has been deleted or added when rebuilding the project.
set(HELLO_LIB_SRC
  hello.cc
)
add_library(hello ${HELLO_LIB_SRC})

set(MAIN_SRC
  main.cc
)
add_executable(hello_bin ${MAIN_SRC})
target_link_libraries(hello_bin hello)

      



Headers /hello.h

#pragma once

#include <string>

namespace nope
{
  std::string hello_there();
}

      

Source / hello.cc

#include <hello.h>

namespace nope
{
  std::string hello_there()
  {
    return "Well hello there!";
  }
}

      

Source / main.cc

#include <hello.h>
#include <iostream>

int main()
{
  std::cout << nope::hello_there() << std::endl;
  return 0;
}

      

Don't worry about placing files in the build folder. It's for the setup phase to figure out.

$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make

      

+4


source







All Articles