I have the following project structure
MainProj
|-SubDir1
| |-Docs
| |-CMakeLists.txt
| |-Src
| |-CMakeLists.txt
|-SubDir2
| |-Src
| |-CMakeLists.txt
|-CMakeLists.txt
In my Mainproj cmakeList.txt I am using
Add_subdirectory(subDir1)
Add_subdirectory(subDir2)
So now I want to exclude the mainproj/subdir1/doc from the build process. But I couldn't find a direct way of doing the same in CMake scripts. Any help in this regard would be appreciated.
I have troubles resolving my module imports. Here is my file structure:
.
|-- scenes
| |-- libs
| | |-- mod.rs
| | `-- components.rs
| |-- mod.rs
| `-- scene.rs
`-- main.rs
I can't import the module libs in scene.rs. I think I don't get it the module logical. Any help would be very appreciable.
if I try to do mod libs; in scene.rs
error[E0583]: file not found for module `libs`
--> src/scenes/scene.rs:2:5
|
2 | mod libs;
| ^^^^
|
= help: name the file either scene/libs.rs or scene/libs/mod.rs inside the directory "src/scenes"
Contents files:
main.rs
mod scenes;
let sc = scenes::scene::Scene{};
scenes/scene.rs
mod libs; // errors
pub struct Sphere {
pub center: libs::components::Point
}
pub struct Scene {}
scenes/mod.rs
pub mod scene;
pub mod libs;
scenes/libs/components.rs
pub struct Point {}
scenes/libs/mod.rs
pub mod components;
Instead of mod libs, write use crate::scenes::libs.
The Rust 2018 Edition has changed the module system slightly to help clarify situations such as this.
Your directory should be restructured like so:
main.rs
scenes.rs
scenes
| libs.rs
| libs
| | components.rs
| scene.rs
The main difference here is that mod.rs files are now extracted from their folder and named appropriately.
PRE-RUST-2018 - NOV 2018
Instead of mod libs, write use scenes::libs.
The error message is telling you is you're trying to declare the existence of a submodule of scene that does not exist. Instead, you want to import (with use) the libs module which is accessed by scenes::libs from the crate root.
I have an Android Studio project with a general layout as follows:
Project/
|
+-- app/
| |
| +-- src/main/cpp/
| | |
| | +-- native-lib.cpp
| | +-- CMakeLists.txt
| |
| +-- build.gradle
| +-- CMakeLists.txt
| |
+-- sibling-lib/
| |
| +-- src/main/cpp/
| | |
| | +-- partA/CMakeLists.txt
| | +-- partB/CMakeLists.txt
| | +-- partC/CMakeLists.txt
| |
| +-- build.gradle
| +-- CMakeLists.txt
|
+-- build.gradle
+-- settings.gradle
sibling-lib is a third-party library comprised of multiple parts, each with separate sources and common headers, each of which is compiled into either a static or shared library, using the associated CMake file, and included as a subdirectory by the top-level library CMake file. Most of those parts are dependent on other parts.
The goal is to compile and link the parts of sibling-lib into library files within an Android Library (.aar) and use those libraries from the native files within the main app (e.g. native-lib.cpp). I have been able to compile each of the parts as a static library, with the final part linked together as a shared library (using target_link_libraries() on that part), and then load the library from the app's Java code. However, when I try to call any of sibling-lib's functions, I receive reference errors when compiling.
How should I configure the CMake and gradle files (for both the app and library) to properly compile and set up my project? Is there, however, a better way to configure the project to obtain the desired result?
I've started using CMake (2.8.12.2, the one included in CentOS 6.8) recently, and I think it's powerful enough to help acomplish what I want, but I haven't been able to figure out how :-), so I call for your wisdom to help me find the missing point.
I have a project layout like this:
BaseDir
|
+-->bin (generated by the process)
| |
| +-->Debug
| +-->Release
|
+-->lib (generated by the process)
| |
| +-->Debug
| +-->Release
|
+-->CMakeLists.txt
|
+-->Library_A
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| | |
| | +-->CMakeLists.txt
| |
| +-->test # Small binary to test solely the library functions
| |
| +-->CMakeLists.txt
|
+-->Library_B (depends on Library_A)
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| | |
| | +-->CMakeLists.txt
| |
| +-->test # Small binary to test solely the library functions
| |
| +-->CMakeLists.txt
|
+-->Application_1 (depends on Library_B, hence transitivitely depends on Library_A)
| |
| +-->CMakeLists.txt
| +-->include
| +-->src
| |
| +-->CMakeLists.txt
|
+-->Application_2 (depends on Library_A)
|
+-->CMakeLists.txt
+-->include
+-->src
|
+-->CMakeLists.txt
It works like a charm when I place myself under BaseDir and run "cmake .". Application_1, Application_2, Library_A and Libray_B are all built in the proper order, linked, etc.
However, my idea is to be also able to build while standing under any of the subdirectories (Application_, Library_), and in that case, build only the code relevant to it (meaning itself, its tests and its dependencies). For example, while standing inside Library_A, only that folder is build, while from Library_B, Library_A is also built, and the equivalent that happens when standing under Application_1 or Application_2. Plus, indepedently on where I'm standing to trigger the cmake process, the build results (libs or bins) must always placed under BaseDir/{lib|bin}/{Debug/Release/etc}, never under the libraries or applications subdirectories. It implies, for example, that the linking of Library_B (which depends on Library_A) or Application_1 (which depends on Library_A and B) must look into BaseDir/lib/{Debug/Release}.
My goal is to have many App's under BaseDir, so I want to avoid having to build them all every time if that's not really necessary, just the single App I want.
I've looked into CMakeLists.txt files for multiple libraries and executables and also CMake and finding other projects and their dependencies, but that's not really the same situation than what I'm trying to achieve here.
I've tried something like the following:
BaseDir/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
| project (BASE_DIR)
|
| add_subdirectory (Library_A) # No local dependencies
| add_subdirectory (Library_B) # Depends on A
| add_subdirectory (Application_1) # Depends on A and B
| add_subdirectory (Application_2) # Depends on A
|
| # I want all binary outputs (executables and libraries) placed under BaseDir/{lib|bin}/{Debug|Release}
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}") # For the executables
| set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
|
+-->BaseDir/Library_A/CMakeLists.txt (again, no dependencies)
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (LIB_A)
| |
| | # In case CMake is run from within BaseDir/Library_A and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
| | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
| |
| | include_directories(include)
| | add_subdirectory (src)
| | add_subdirectory (test)
| | set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
| |
| +-->BaseDir/Library_A/src/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| |
| | # add all files in the current directory
| | file(GLOB LIB_A_SRCS "*.h" "*.cpp")
| |
| | # Create a library called libA.a
| | add_library(A ${LIB_A_SRCS})
| |
| +-->BaseDir/Library_A/test/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB TEST_A_SRCS "*.h" "*.cpp")
|
| # Create an executable file from sources
| add_executable(TEST_A ${TEST_A_SRCS})
|
| # Link this executable to the library it's testing
| target_link_libraries(TEST_A A)
|
+-->BaseDir/Library_B/CMakeLists.txt (dependency on A)
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (LIB_B)
| |
| | # In case CMake is run from within BaseDir/Library_B and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
| | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
| | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
| |
| | include_directories(include)
| | add_subdirectory (src)
| | add_subdirectory (test)
| | set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
| |
| +-->BaseDir/Library_B/src/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| |
| | # add all files in the current directory
| | file(GLOB LIB_B_SRCS "*.h" "*.c")
| |
| | # Create a library called libB.a
| | add_library(B ${LIB_B_SRCS})
| |
| | # Add a dependency to Library_A
| | find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_A)
| | include_directories("$LIB_A_INCLUDE_DIRECTORIES")
| | target_link_libraries(B ${LIBRARY_A})
| |
| +-->BaseDir/Library_B/test/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB TEST_B_SRCS "*.h" "*.cpp")
|
| # Create an executable file from sources, for both versions of the library
| add_executable(TEST_B ${TEST_B_SRCS})
|
| # Link this executable to the library it's testing
| target_link_libraries(TEST_B B)
|
+-->BaseDir/Application_1/CMakeLists.txt
| |
| | cmake_minimum_required (VERSION 2.8)
| | project (APP_1)
| |
| | # In case CMake is run from within BaseDir/Application_1 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| | # In this case, only executables are generated.
| | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
| |
| | include_directories(include)
| | add_subdirectory (src)
| |
| +-->BaseDir/Application_1/src/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
|
| # add all files in the current directory
| file(GLOB APP_1_SRCS "*.cpp")
|
| # Create an executable file from sources
| add_executable(EXE_1 ${APP_1_SRCS})
|
| # This should automatically bring Library_A
| find_library(LIBRARY_B B PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_B)
| include_directories(${LIB_B_INCLUDE_DIRECTORIES})
| target_link_libraries(EXE_1 ${LIBRARY_B})
|
+-->BaseDir/Application_2/CMakeLists/CMakeLists.txt
|
| cmake_minimum_required (VERSION 2.8)
| project (APP_2)
|
| # In case CMake is run from within BaseDir/Application_2 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
| # In this case, only executables are generated.
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
|
| include_directories(include)
| add_subdirectory (src)
|
+-->BaseDir/Application_2/src/CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
# add all files in the current directory
file(GLOB APP_2_SRCS "*.cpp")
# Create an executable file from sources
add_executable(EXE_2 ${APP_2_SRCS})
# Link this executable to the library it needs
find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}../../Library_A)
include_directories(${LIB_A_INCLUDE_DIRECTORIES})
target_link_libraries(EXE_2 ${LIBRARY_A})
But with no luck, because when I run from the subdirectories (for example, BaseDir/Application_1), I just get:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBRARY_B
I guess the "find_library()" call is not enough for what I want, which is basically to load all the settings contained in the libraries project, including not only the library itself, but also the include's directories added by that library. What is exactly the way of usage for find_library(), to point it to the location of the project creating the library, or the actual resulting library (".a" or ".so")?
So, my main doubt is: Is this layout doable? If so, what am I missing? Should I use something like find_package() or find_path()? How do I trigger the parsing of a CMakeLists.txt config file from another "same level" CMakeLists.txt?
PS: Do I really need to consider using "add_dependencies()" at any point? What's the point of that command if not?
First a general piece of advice: In CMake you will always want try to strictly separate your build directories from your source directories. In particular, you should never write files back to the source directory as part of the build process. Write your scripts so that they will also work with the source directory being mounted on a read-only file system. While this might seem like an arbitrary restriction at first, it actually will help you avoid many headaches in the long run.
As for your actual problem: find_library is a very low-level tool for solving the dependency problem. The idea here is basically that you have binaries for a third-party library (which itself knows nothing about CMake) installed somewhere on the system and you know need to find them simply by inspecting the contents of the filesystem. This is not the case here, as you build the dependencies as part of the same CMake configure run.
What you want to do instead is directly depend on the targets for the dependency. So instead of doing this:
find_library(LIBRARY_A A)
target_link_libraries(B ${LIBRARY_A})
you would directly write
target_link_libraries(B A)
This of course only works if A is a known target at that point, and this is the problem that you should focus on.
As long as you keep building the libraries together as part of the same CMake run, this is pretty straightforward: If a target is added by a parent or sibling directory, you should be able to use it right away. What complicates things in your situation is that you also want to be able to build the different libraries in isolation. That means you need a mechanism that imports a target into your build so that it looks as if that target was again produced as part of the same CMake run.
You can either do this manually by adding an imported target and setting its interface properties, or you can use CMake's packaging mechanism to have this done in an automated way. Note that neither of these is trivial to pull off, so you might want to start with the simple case where everything happens in one CMake run and then add support for the separated build later once you are more comfortable with using CMake.
I have two projects: Server and Client (in two different folders) - two executable files at the output. But Server uses some sources from Client which are common for both of them. It is supposed that these two folders will be always in one folder, so relative paths are possible.
How can I tell Qt to use them?
I tried to add dependence to Server on Client in Dependencies menu of Project page. Tried to add paths to header and source of common files to *.pro files of Server (at HEADERS and SOURCES) files, but it didn't help (or I do something wrong).
Adding common files to SOURCES and HEADERS sections should be enough.
Here's an example project:
my_project
|-- client.pro
|-- client
| `-- main.cpp
|
|-- server.pro
|-- server
| `-- main.cpp
|
`-- common
|-- common_class.cpp
`-- common_class.h
To both client and server projects use common sources you should and this to the both .pro files:
SOURCES += common/common_class.cpp
HEADERS += common/common_class.h