I have an identical problem to this Gradle Multi-Module Project Setup but I have to use project compile dependencies to build and cannot use the library(jar) dependencies solution that was given as a solution in the above question.
Root
|__ P1
| |_ PP1
| |_ PP2
|
|__ P2
|_PP3
|_PP4
PP1, PP2, PP3 and PP4 are sub-projects and each have their own build.gradle files; P1 and P2 also have build.gradle and settings.gradle files.
How can I declare PP1 as a compile dependency in PP3's build.gradle file?
apply plugin: 'java'
dependencies {
compile('P1:PP1') //does not work
compile group: 'P1', name: 'PP1', version: '0.1' // jar library dependency not an option
compile('{ant_target}')? //follow up question - an ant target as a dependency
}
I'm running Gradle v1.2
A build can only have a single settings.gradle file. Assuming settings.gradle is in the root directory and includes projects like so:
include "P1:PP1"
You can add a compile dependency like so:
dependencies {
compile(project(":P1:PP1"))
}
For more information, see the "Multi-Project Builds" chapter in the Gradle User Guide.
Related
I'd like to use a few classes from the LLVM project in my own Objective-C app. Specifically, I want to use the classes declared in BitstreamReader.h and BitstreamWriter.h. Unfortunately, I don't have much experience linking C++ libraries, so I don't really know where to begin. I started by installing llvm through Homebrew with brew install llvm#14. Then, in my Xcode project, I tried linking the libraries in /opt/homebrew/opt/llvm#14/lib, and adding /opt/homebrew/opt/llvm#14/include/** to my HEADER_SEARCH_PATHS.
Now I'm completely stuck. I have a bunch of build errors in Xcode like:
"Reference to unresolved using declaration"
"No member named 'ldiv' in the global namespace"
"Use of undeclared identifier 'wcspbrk'"
...
Any help at all would be greatly appreciated. Thanks!
In C++ world there are quite a few ways to incorporate a dependency into your project, I'll outline some of them, but will try to describe in details one which I find the most suitable for your scenario:
1. Installing libraries system/user-wide
This is the most classic approach, when you have the libraries pre-compiler and pre-installed for your specific platform so they are available under default library search-path. It's similar to how system frameworks in iOS are used - you only add linker command to the project, while the frameworks (libraries) exist independently from it (or don't exist, which causes linker error). The problem with this approach, is that you cannot really use the libs for restricted systems, where "default" libraries are unchangeable (iOS, tvOS, iPadOS)
2. Embedding static libraries
Another approach is to precompile all libraries you need into archives for all platforms the libraries are supposed to be used and then just embed one of the library version which is required into your final app binary. This approach is somewhat cumbersome and unportable because it will require you to compile the lib and re-embed it manually each time you need some parts of library changed or new platform supported.
3. Embedding an xcframework
This approach is very similar to the previous one with a few benefits that you can wrap binaries for all platforms under one package and even release it in SPM.
4. Using CMake build system
Many projects (and LLVM is not an exception) in C++ are made with use of so-called CMake tool. It's widely adopted multi-platform build system and one of the benefits you can employ is that you can easily include any other project as part of your own. At the same time CMake is not widespread at all in the world of mobile development, so you may have hard time finding relevant information for these platforms.
5. Using Xcode workspaces
This is the solution I'd like to describe in more details. In a nutshell this approach consists of 6 steps:
Download LLVM project repo;
Generate Xcode project for required LLVM libraries with use of CMake;
Make an Xcode workspace and add the newly generated project to it;
Add your own project to the same workspace;
Add required dependencies from LLVM project to your own;
Adjust project settings to make it compatible with the dependencies.
The benefits of this approach is that it gives the most consistent experience (after generating project, all parts are configurable from Xcode), freedom of managing the source code (the LLVM project files can be altered however you want), and luxury of built-in dependency graph (all libraries required to build a target are given under Xcode settings).
Downloading LLVM project
You can clone the project from here. Be advised that this has to be in the same folder your future workspace will be in, so you better prepare it in advance:
% mkdir MyWorkspace && cd MyWorkspace
% git clone git#github.com:llvm/llvm-project.git
Generating LLVM Xcode Project
First, ensure you have CMake installed (it doesn't come out of the box with macOS). You can use brew or just download the app from the official site. After that create a new folder for the future Xcode project next to llvm-project repo folder and run cmake on the LLVM toolkit project:
% mkdir LLVM && cd LLVM
% cmake -GXCode ../llvm-project/llvm
Creating Xcode workspace
Nothing fancy here, just open Xcode and navigate to File/New/Workspace menu (Ctrl+Cmd+N shortcut). Ensure that your workspace is created in the MyWorkspace folder. You can just do it from Xcode:
Then, add the LLVM project created on the previous step. Open File/Add Files to "MyWorkspace"... menu (Option+Cmd+A) and select the Xcode project file:
If Xcode suggests to auto-create schemes I recommend accepting this suggestion so you don't have to deal with it yourself later on.
Adding your own project
This step mimics the previous one with exception that you add your own project to the workspace. I didn't have an existing project for that, so I just created a new one (in my case macOS command-line app) in the workspace directory. If you do the same, ensure that the project is added to "MyWorkspace" and the folder is correct:
Eventually your workspace "Project Navigator" should look something like this:
And here is how the directory tree looks like in a nutshell:
% tree -L 2
.
|-- LLVM
| |-- $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
| |-- CMakeCache.txt
| |-- CMakeFiles
| |-- CMakeScripts
| |-- CPackConfig.cmake
| |-- CPackSourceConfig.cmake
| |-- Debug
| |-- LLVM.xcodeproj
| |-- MinSizeRel
| |-- RelWithDebInfo
| |-- Release
| |-- benchmarks
| |-- build
| |-- cmake
| |-- cmake_install.cmake
| |-- docs
| |-- examples
| |-- include
| |-- lib
| |-- llvm.spec
| |-- projects
| |-- runtimes
| |-- test
| |-- third-party
| |-- tools
| |-- unittests
| `-- utils
|-- MyProject
| |-- MyProject
| `-- MyProject.xcodeproj
|-- MyWorkspace.xcworkspace
| |-- contents.xcworkspacedata
| |-- xcshareddata
| `-- xcuserdata
`-- llvm-project
|-- CONTRIBUTING.md
|-- LICENSE.TXT
|-- README.md
|-- SECURITY.md
|-- bolt
|-- clang
|-- clang-tools-extra
|-- cmake
|-- compiler-rt
|-- cross-project-tests
|-- flang
|-- libc
|-- libclc
|-- libcxx
|-- libcxxabi
|-- libunwind
|-- lld
|-- lldb
|-- llvm
|-- llvm-libgcc
|-- mlir
|-- openmp
|-- polly
|-- pstl
|-- runtimes
|-- third-party
`-- utils
Adding dependencies to your project
From this point forward you'll only need Xcode to finish the job. First, let's link the libraries you need to the project and start from the Bitstream archive. Open your project's target General tab settings and under Framework and Libraries click the + sign which should take you to the screen with all local dependencies currently available. It's quite a long list of tools from the LLVM project + native Apple libs, so you may want to filter it as needed to find the required position:
Now the tricky part. You might not actually be required to do that, but if the LLVMBitstreamReader target itself relies on symbols of other libraries (and exposes such a dependency with explicit use of the libraries symbols) the project linking will fail, so to be safe go to the LLVMBitstreamReader build settings and check what it depends on under Target Dependencies section:
Now add these dependencies to your project as well. Eventually your Frameworks and Libraries section should look like this:
Adjusting project settings
CMake generated Xcode projects come with peculiarities. In our case the build folders for the LLVM project targets are located under the project directory with the name matching selected configuration (Debug/Release/MinSizeRel/RelWithDebInfo). In order for your own project's targets to find the libraries built with the given configuration you either have to adjust the LLVM project/targets settings, which I don't recommend because it's a lot of manual work, or just add custom library search path to your project. For the latter go the Build Settings of the target where you added the dependencies at the previous step, find Library Search Path item and add $(SRCROOT)/../LLVM/$(CONFIGURATION)/lib as a new item:
Another tricky part is that dependent targets won't make dependencies to build if the dependencies' targets have incompatible build settings. Incompatible is a vague term here, but commonly it means matching the Architectures section. Luckily in our case it merely means making Release configuration to build active architecture only (the Debug shouldn't require any changes at all):
Last, but not least, the headers of the library. After generating the LLVM Xcode project, the headers it offers don't (completely) come with the project and are actually located in the repository folder. You may just copy the headers right into your own project, but I suggest adding the repo directory in the System Headers Search paths to stay consistent with the LLVM Xcode Project settings (you can yourself check where the headers search paths are under the the LLVMBitstreamReader Build Settings). Similarly to lib search path, I suggest use help of settings variables and add this path flexibly:
$(SRCROOT)/../llvm-project/llvm/include
$(SRCROOT)/../LLVM/include
Finale
At this point you should be good to use the LLVMBitstreamReader library and classes defined there. I renamed my main.m to main.mm so clang knows i'm going to use C++ in my code, and here is my sample code:
//
// main.mm
// MyProject
//
// Created by Aleksandr Medvedev on 14.12.2022.
//
#import <Foundation/Foundation.h>
#import <llvm/Bitstream/BitstreamReader.h>
#import <iostream>
int main(int argc, const char * argv[]) {
llvm::BitstreamBlockInfo::BlockInfo info;
info.Name = "Hello, LLVM!";
std::cout << info.Name << std::endl;
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
}
return 0;
}
If everything is done right, it should compile without any errors now.
P.S. I many times focused on specific directory hierarchy for a reason - when an XCode project is generated through CMake it often uses absolute paths, not relative and if you move your project to another directory, you will have to either adjust the paths in the build settings of required targets/projects or repeat the step with generating the LLVM Xcode project again.
I would like to configure correctly project's settings.json file to use CMakeLists.txt file of multiple sub-project.
My approach is given below
The structure of the project is as like as follows.
vscode_build_cmake/
`-- task
|-- add
| |-- CMakeLists.txt
| |-- include
| | `-- foo.h
| `-- src
| |-- foo.cpp
| `-- main.cpp
`-- print
|-- CMakeLists.txt
|-- include
| `-- foo.h
`-- src
|-- foo.cpp
`-- main.cpp
Here, add & print are completely isolated sub-project of the main project task. Each sub-project has their own build recipe which is written in their own CMakeLists.txt file. Usual steps to build sub-project is :
cd task/add
mkdir build
cd build
cmake .. && make
Same steps is for task/print.
Right now I would like to perform this build task using VSCode Cmake tool. What I have found that I only need to make a vscode_build_cmake/.vscode/settings.json file where I can point to the directory of CMakeLists.txt file. I am now working with
{
"cmake.configureOnOpen": false,
"cmake.sourceDirectory": "${workspaceFolder}/task/add"
}
I just press ctrl+shift+p and write in the command box run build task and press Enter. That one do the job for me and I can see build, bin, lib directories inside task/add directory.
But the problem, I am facing now is that if I need to build print sub-project I have to manually edit the settings.json file as like as follows
{
"cmake.configureOnOpen": false,
"cmake.sourceDirectory": "${workspaceFolder}/task/print"
}
After that, I have to delete cache and reconfigure and build task which perform now the build task only for print project. This one is very tedious to me.
My desire is to know
Is there any way to point out the both CMakeLists.txt file of add and print in cmake.sourceDirectory? Which can tell me which build task I would like to perform? Eg:
{
"cmake.configureOnOpen": false,
"cmake.sourceDirectory": ["${workspaceFolder}/task/add",
"${workspaceFolder}/task/print"]
}
Or ,
Is there any way to perform build task of both sub-project.(this one is optional. Previous one is important for my case).
I am using CMake Tools v1.13.4.
If you're intent on not having a tasks/CMakeLists.txt that add_subdirectorys add/ and print/, then you can consider using VS Code's multi-root workspace feature, which the vscode-cmake-tools extension supports.
If you have a reason not to use / that you don't like the multi-root workspace approach, you can show your support for vscode-cmake-tools issue #1374 ("Support Multiple CMakeLists.txt without Requiring a Multi-Root Workspace") and the proposed solutions. At the time of this writing, the proposal process is still in earlier stages.
Consider this project structure:
Complete example of the project: another-ghasem/CMakePluginProblem
+
|
+-- plugin/
+- plugin_1/
+- plugin_2/
+- plugin_3/
+-- CMakeLists.txt
+-- main.cpp
Those plugins are Shared Libraries created with CMake and will be linked to the root project. But the plugins dependencies are the problem (all the plugins have a Target Alias with BUILD_INTERFACE include path: plugin_2/CMakeLists.txt).
How could we manage the non-generated but linked plugins?
Consider the plugin_1 linked to plugin_2 (plugin_1/CMakeLists.txt#22):
target_link_libraries(${PROJECT_NAME} PRIVATE Project::plugin_2)
Here CMake could somehow handle the build order, but if we remove the plugin_2 CMake will raise an error about it:
Target "plugin_1" links to target "Project::plugin_2" but the target was
not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?
By the way, we couldn't use an if-statement to check "if we have the needed plugin, then linked and define a macro to notice that we have the plugin". How could we handle these dependencies?
- CMake Version: 3.16
- OS: Fedora 34, Ubuntu 18.04
I'm pretty new to C++. I've found a hash library I want to use: https://github.com/aappleby/smhasher
What is the right way of including that library in my own code?
Is it common to clone other repos directly into a lib folder and build the code within?
My folder structure is currently:
|-include
| |- myfile.h
|-src
| |- myfile.cpp
| |- main.cpp
|-lib
| |-smhasher
| |- MurmurHash3.h
| |- MurmurHash3.cpp
| ... etc
|- Makefile
and my makefile currently builds MurmurHash3.cpp before building myfile.cpp before building main.cpp
It's a bit weird to me because this setup needs to include the smhasher library in my source code when pushing to github for version control. I can gitignore the lib folder but then ppl using my code need to be aware they are required to clone smhasher into the lib folder.
Alternatively I'm wondering if I should go into the smhasher source code and build a static library and include that in a folder here.
Basically I'm wondering what is the most common way to handle external dependencies in c++? Are there benefits of doing it one way or the other?
The most common way would probably be to clone the repo, put .hinto /include/ and .cpp into /src/. Then adjust makefile accordingly.
I started playing around with CMake to create a project with Qt and test it with Google Test. At the moment, I succesfully found a way to compile and link all the required libraries. However, I couldn't find a way to link sources to test files with following project structure:
root
|
+-- CMakeLists.txt
+-- src
| |
| +-- CMakeLists.txt
| +-- MyClass.h
| +-- MyClass.cpp
|
+-- test
| |
| +-- CMakeLists.txt
| +-- MyClassTest.cpp
|
+-- lib
|
+-- gtest-1.6.0
|
+-- CMakeLists.txt
Root CMakeLists.txt contains add_subdirectory for gtest, src and test folders. I have succesfully compiled and run "Hello world" app and simple EXPECT_TRUE(true) test in order to check that each part compiles correctly. Unfortunately, I couldn't find a way to include my source file to tests. Is it possible with the following project structure?
PS I know that it is possible to compile my sources as a library and link it to tests, but I dislike that approach, since it is more appropriate for integration testing, rather then unit testing...
EDIT: Added class names to the tree
You can add a global variable at the level of your root CMakeLists.txt:
set(ALL_SRCS CACHE INTERNAL "mydescription" FORCE)
In the first add_subdirectory(src), you can do:
set(ALL_SRCS ${ALL_SRCS} blabla.cpp CACHE INTERNAL "description")
And in the add_subdirectory(test), you continue with:
set(ALL_SRCS ${ALL_SRCS} bla_test.cpp CACHE INTERNAL "description")
You can then do, add_executable, or library or whatever, with all your sources files.
EDIT: add trick for global variables in CMake.
In the root CMakeLists.txt you can add a include_directories(src) This will then also be used by the tests. Another thing you can do is in the test CMakeLists.txt add a include_directories(${<projectName>_SOURCE_DIR}) where projectName is the name specified using project(myproj) in the src/ CMakeLists.txt (if you specified a project in there of course. Also check the docs about project)