My source project directory is something like this,
|── CMakeLists.txt
|
├── src
| |
| |
| |
| └── a
| | |
| | |__ a.cpp
| |
| |
| |
| └── b
| |
| |__ b.cpp
|
|___test
|
|__test.cpp
I need to make a executable out of source a.cpp and b.cpp,
file(GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp)
I expect this to take a.cpp and b.cpp as the only source files, but when I print
message("All source file - ${SRC_FILES}")
It picks up test.cpp as well, which I don't know why it's happening. My understanding here is ${CMAKE_CURRENT_SOURCE_DIR}/src/*/ *.cpp, in this statement
due to wildcard * it would go through each subdirectory of src folder, and then with *.cpp it would append all the cpp files inside each of these subdirectory.
Related
I'm trying to write Makefile script which will compile my project in VS Code which contains sub directories. My project structure looks like:
src
| - subdirectory1
| | - file1.cpp
| | - file2.cpp
| - subdirectory2
| | - sub3
| | | - file4.cpp
I am new to makefile concept and I Having this template library how can i create makefile to install this library on system, can i create makefile for template library.
Source code Folder structure(omitted some files):
artlib - c++
|
|--- include
| |
| |--- artlib.hpp
|
|--- source
|
|---- anixt
| |
| |--- anixt_config.hpp
| |--- anixt_font.hpp
| |--- anixt_style.hpp
| |--- basic_anixt.hpp
|
|---- runeape
|
|--- basic_runeape.hpp
|--- runeape_config.hpp
|--- runeape_gallery.hpp
|--- runeape_style.hpp
|-------- third_party/json/json.hpp
Now, How to install this files and artlib.hpp file includes all other file and have some type alias and some functions. I am currently build test
code by -I option in g++ and include artlib.hpp in source file
All suggestions are welcome,
Thanks in advance.
Here's a makefile to install a file template.h into /usr/local/include:
FILES := template.h
DESTDIR := /usr/local/include
INSTALLEDFILES := $(addprefix $(DESTDIR)/,$(FILES))
all: $(INSTALLEDFILES)
$(DESTDIR)/%: %
mkdir -p $(#D)
cp $< $#
Since you've not given us any actual details about what you want to do, that's about all we can say.
project
|
|----- src <-- Needs to be a library
| |
| |--- dir1 <-- Need not create a library
| | |--- dir1_1.cpp
| | |--- dir1_2.cpp
| | |--- CMakeLists.txt
| |
| |
| | --- dir2 <-- has an executable for testing and needs to be a library
| | |--- dir2.cpp
| | |--- dir2.h
| | |--- CMakeLists.txt
| |
| |
| | --- CMakeLists.txt
|
|
|
|----- CMakeLists.txt
How do I create CMakeLists in src such that it includes files from dir1 and dir2, such that only dir2 is a sub-project. I want to use add_subdirectory in src/CMakeLists.txt to add source files from dir1 and dir2. This way it will be easier to add more folders in the future.
I saw the use of PARENT_SCOPE but it also has some drawbacks. Is there a better solution? Maybe by using target_sources or something?
First of all, you should reconsider not making those subdirs self-contained modules, as modularity helps keep your projects cleaned.
Having said that, you can include in any variable defined in ./src/CMakeLists.txt any variable that's defined by ./src/dir1/CMakeLists.txt or ./src/dir2/CMakeLists.txt. Here's an example:
Here are the contents of ./src/dir1/CMakeLists.txt:
set(dir1_SOURCES
dir1/dir1_1.cpp
dir1/dir1_2.cpp
)
Here are the contents of ./src/CMakeLists.txt:
add_subdirectory(dir1)
set(src_SOURCES
${dir1_SOURCES}
)
You can create a library, then later add its source files in a subdirectory using target_sources. This gives you the flexibility to add more sources to MyLib or add more sub-projects later on, via additional calls to add_subdirectory.
It might look something like this:
src/CMakeLists.txt:
# Create a library
add_library(MyLib SHARED)
# Traverse to the 'dir1' subdirectory to populate the sources for MyLib.
add_subdirectory(dir1)
# Traverse to 'dir2' to create a sub-project.
add_subdirectory(dir2)
dir1/src/CMakeLists.txt:
target_sources(MyLib PUBLIC
dir1_1.cpp
dir1_2.cpp
)
# Add any headers from this directory.
target_include_directories(MyLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
dir2/src/CMakeLists.txt:
project(MySubProject)
add_library(MyLib2 SHARED
dir2.cpp
)
# Add any headers from this directory.
target_include_directories(MyLib2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(MyExe ...)
...
The problem is solved, it is due to other dependency of my project, it has nothing wrong with cmake. Thanks for everyone trying to help in the question.
Here is my project tree Tree 1:
project
| + src
| | + other_src
| | | - abc.cpp
| | | - abc.h
| | - main.cpp
| - CMakeLists.txt
3rd_party
| + pcap
| | - pcap.h
| - pcap.h
Inside my CMakeLists.txt, I do:
include_directories("/path/to/3rd_party")
Inside my abc.cpp, I have:
#include "pcap.h"
Then I do cmake and make. I get error pcap.h: No such file or directory
I then change my project tree Tree 2:
project
| + src
| | - abc.cpp
| | - abc.h
| | - main.cpp
| - CMakeLists.txt
3rd_party
| + pcap
| | - pcap.h
| - pcap.h
Without modifying my CMakeLists.txt, I do cmake and make again. The header file is found.
How can I build this project while my source code is placed in different folders?
I am currently starting a new project using C++ and CMake,
in my last project I organized the code the following:
src
|--foo
| |--bar.cpp
| |--bar.hpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.cpp
| | |--Bahama.hpp
| | |--CMakeLists.txt
| |--baz.cpp
| |--baz.hpp
| |--CMakeLists.txt
|--CMakeLists.txt
so every subdirectory got its own CMakeLists.txt, which was included as library from other CMakeList files.
Now I want to separate the .hpp and the .cpp files.
Something like this:
src
|--foo
| |--bar.cpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.cpp
| | |--CMakeLists.txt
| |--baz.cpp
| |--CMakeLists.txt
|--CMakeLists.txt
include
|--foo
| |--bar.hpp
| |--CMakeLists.txt
|--baz
| |--anotherFoo
| | |--Bahama.hpp
| | |--CMakeLists.txt
| |--baz.hpp
| |--CMakeLists.txt
|--CMakeLists.txt
What I am supposed to do with the CmakeLists files?
Do I also need a CMakeLists file in every subdirectory, like I did in my example above? Or is there another way to do this?
What do I write in this CMake files
In the future I also want to add documentation with doxygen, how can I enable Doxygen support via Cmake in a structure like this?
should also every subdirectory get its own?