I would like to understand how to structure my cpp project correctly. I am using the build generator CMAKE. The build system I am using is Ninja. I have 2 main functions in my project. Each main should be compiled into a different executable.
When and why should I use multiple cmake files?
How can I better structure my project?
|-- CMakeLists.txt
|-- README.md
|-- env.csh
|-- include
| |-- Pen.h
| |-- Cup.h
| |-- Clip.h
| |-- Fun.h
| |-- Ins.h
| |-- Ne.h
| `-- Pa.h
|-- libs
|-- src
| |-- Pen.cpp
| |-- Cup.cpp
| |-- Clip.cpp
| |-- Fun.cpp
| |-- Ins.cpp
| |-- Ne.cpp
| |-- Pa.cpp
| |-- main0.cpp
| `-- main1.cpp
`-- tests
`-- test.cpp
You need one add_executable() line for each executable in your project. Try this CMakeLists.txt file (written mostly from memory):
cmake_minimum_required(VERSION 2.8)
project(myproject LANGUAGES CXX)
enable_testing()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SOURCES
src/Pen.cpp
src/Cup.cpp
src/Clip.cpp
src/Fun.cpp
src/Ins.cpp
src/Ne.cpp
src/Pa.cpp
)
add_executable(main0 src/main0.cpp ${SOURCES})
add_executable(main1 src/main1.cpp ${SOURCES})
add_executable(unittests tests/test.cpp ${SOURCES})
add_test(tests unittests)
Related
Let's say I have such project structure
|-- DynamicLoader
| `-- dmc
| |-- DynamicLoader.h
| |-- Loader
| | |-- DynamicLibrary.cpp
| | |-- DynamicLibrary.h
| | |-- LibraryManager.cpp
| | |-- LibraryManager.h
| |-- Utils
| | |-- Utils.h
| |-- dmc.cpp
| |-- dmc.h
where dmc.h is my pre-compiled header.
I want to use it for example in my source files located in Loader folder like this: #include "dmc/dmc.h
but the intelisense complains about it and I have to use those semantics:
#include "../dmc.h"
is there anyway to achieve this in visual studio? I've seen one project that worked similarly to this but I couldn't figure it out by myself
This is the directory structure of my project:
|-- CMakeLists.txt
|-- libdashframework
| |-- Buffer
| | |-- IMediaObjectBufferObserver.h
| | |-- MediaObjectBuffer.cpp
| | `-- MediaObjectBuffer.h
| |-- Input
| | |-- DASHReceiver.cpp
| | |-- DASHReceiver.h
| | |-- IDASHReceiverObserver.h
| | `-- MediaObject.h
| |-- MPD
| | |-- AbstractRepresentationStream.cpp
| | |-- AbstractRepresentationStream.h
| | |-- AdaptationSetStream.cpp
| | |-- AdaptationSetStream.h
| | |-- BaseUrlResolver.cpp
| | |-- BaseUrlResolver.h
| | |-- IRepresentationStream.h
| | |-- RepresentationStreamFactory.cpp
| | |-- RepresentationStreamFactory.h
| | |-- SegmentListStream.cpp
| | |-- SegmentListStream.h
| | |-- SegmentTemplateStream.cpp
| | |-- SegmentTemplateStream.h
| | |-- SingleMediaSegmentStream.cpp
| | |-- SingleMediaSegmentStream.h
| | |-- TimeResolver.cpp
| | `-- TimeResolver.h
| `-- Portable
| |-- MultiThreading.cpp
| `-- MultiThreading.h
|-- libdash_test_automoc.cpp
|-- libdash_test.cpp
`-- Makefile
I am having a linking error in the project and can't figure out how to remove it.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
file(GLOB test_source RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
libdashframework/MPD/*.cpp
libdashframework/Buffer/*.cpp
libdashframework/Input/*.cpp
libdashframework/Portable/*.cpp
libdash_test.cpp )
add_executable(libdash_test ${test_source})
target_link_libraries(libdash_test dash -lpthread)
Error:
Linking CXX executable libdash_test
CMakeFiles/libdash_test.dir/libdashframework/Buffer/MediaObjectBuffer.cpp.o: In function `libdash::framework::buffer::MediaObjectBuffer::SetEOS(bool)':
MediaObjectBuffer.cpp:(.text+0x55d): undefined reference to `libdash::framework::input::MediaObject::AbortDownload()'
CMakeFiles/libdash_test.dir/libdashframework/Input/DASHReceiver.cpp.o: In function `libdash::framework::input::DASHReceiver::GetNextSegment()':
DASHReceiver.cpp:(.text+0x48e): undefined reference to `libdash::framework::input::MediaObject::MediaObject(dash::mpd::ISegment*, dash::mpd::IRepresentation*)'
CMakeFiles/libdash_test.dir/libdashframework/Input/DASHReceiver.cpp.o: In function `libdash::framework::input::DASHReceiver::GetSegment(unsigned int)':
DASHReceiver.cpp:(.text+0x57f): undefined reference to `libdash::framework::input::MediaObject::MediaObject(dash::mpd::ISegment*, dash::mpd::IRepresentation*)'
CMakeFiles/libdash_test.dir/libdashframework/Input/DASHReceiver.cpp.o: In function `libdash::framework::input::DASHReceiver::GetInitSegment()':
DASHReceiver.cpp:(.text+0x615): undefined reference to `libdash::framework::input::MediaObject::MediaObject(dash::mpd::ISegment*, dash::mpd::IRepresentation*)'
CMakeFiles/libdash_test.dir/libdashframework/Input/DASHReceiver.cpp.o: In function `libdash::framework::input::DASHReceiver::DownloadInitSegment(dash::mpd::IRepresentation*)':
DASHReceiver.cpp:(.text+0x9b0): undefined reference to `libdash::framework::input::MediaObject::StartDownload()'
CMakeFiles/libdash_test.dir/libdashframework/Input/DASHReceiver.cpp.o: In function `libdash::framework::input::DASHReceiver::DoBuffering(void*)':
DASHReceiver.cpp:(.text+0xa84): undefined reference to `libdash::framework::input::MediaObject::StartDownload()'
DASHReceiver.cpp:(.text+0xab5): undefined reference to `libdash::framework::input::MediaObject::WaitFinished()'
collect2: error: ld returned 1 exit status
make[2]: *** [libdash_test] Error 1
make[1]: *** [CMakeFiles/libdash_test.dir/all] Error 2
make: *** [all] Error 2
How can I remove this linking error?
After reading other similar questions, I have tried and changed the order of files in file() function. The order is similar to a working sample project so I think thats not the problem.
EDIT:
Although I had already linked the so file of libdash by putting it in the /usr/bin and /usr/lib/ dir. But I also updated the cmake too. It still didn't help.
updated CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(libdash_test)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS "-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS")
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
#libdash has to be manually installed
set (DASH_INCLUDE_DIR /usr/include/libdash)
find_package(DASH REQUIRED)
link_directories(${DASH_LIBRARIES})
#set(LIBDASH_LIBRARIES "../build/bin" CACHE PATH "Path to libdash.so")
#set(LIBDASH_INCLUDES "../libdash/include/" CACHE PATH "Path to libdash includes")
file(GLOB test_source RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
libdashframework/MPD/*.cpp
libdashframework/Buffer/*.cpp
libdashframework/Input/*.cpp
libdashframework/Portable/*.cpp
libdash_test.cpp )
add_executable(libdash_test ${test_source})
target_link_libraries(libdash_test DASH -ldash ${DASH_LIBRARIES} -lpthread)
Error in the current CMakeLists.txt (the one under EDIT):
CMake Error at CMakeLists.txt:20 (find_package):
By not providing "FindDASH.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "DASH", but
CMake did not find one.
Could not find a package configuration file provided by "DASH" with any of
the following names:
DASHConfig.cmake
dash-config.cmake
Add the installation prefix of "DASH" to CMAKE_PREFIX_PATH or set
"DASH_DIR" to a directory containing one of the above files. If "DASH"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
I am able to locate the specific file by giving the path in Repository().
But the output generated is in the Sconstruct directory instead of in the
variant_dir.
Please let me know how to resolve this issue
Explaining the problem specifically
The tree structure in the project looks as below.
tree .
.
|-- SConstruct
|--Build
| `-- SConscript_unitTest
|--FileB_mock.o
|--FileA.o
|-- _Output
| `-- unit_test
| |--FileA_unittest.o
`-- moduleA
| |-- FileA.cpp
| `-- UnitTest
| |-- FileA_unittest.cpp
| `-- SConscript
`-- moduleB
| |-- FileB.cpp
| `-- UnitTest
| |-- FileB_unittest.cpp
| `-- SConscript
`-- Mocks
`-- moduleA
| |-- FileA_mock.cpp
| `-- FileA_mock.h
`-- moduleB
|-- FileB_mock.cpp
`-- FileB_mock.h
For the above structure,
In the SConstruct i make the call to SConscript_unitTest.
Implementation of Sconscript_unitTest is
'SConscript('#\moduleA\UnitTest\SConscript',
variant_dir = '#_Output\unit_test'), duplicate=0)'
Implementation of moduleA\UnitTest\Sconscript is
Sources = [ 'FileA_unittest.cpp',
'#FileB_mock.cpp',
'#FileA.cpp'
]
Repository( '#\ModuleA', '#\Mocks\ModuleB')
obj_files = Object(Sources)
Program(target_name, obj_files)
As shown in the Heirarchical diagram, FileA_unittest.o falls in the intended variant_dir, but FileB_mock.o and FileA.o are in the root folder.
I am not sure what i have done here is the best mechanism .. as i am translating an existing make project to scons.
Hope i have given enough detail of the problem.
Here's the structure of the folder:
TEST
|-- DIR1
| |-- TEST1.cpp
| `-- TEST1.h
|-- DIR2
| |-- TEST2.cpp
| `-- TEST2.h
`-- main.cpp
After qmake -project, qmake TEST.pro, make, I got:
TEST
|-- DIR1
| |-- TEST1.cpp
| `-- TEST1.h
|-- DIR2
| |-- TEST2.cpp
| `-- TEST2.h
|-- Makefile
|-- TEST
|-- TEST.pro
|-- TEST1.o
|-- TEST2.o
|-- main.cpp
`-- main.o
I want to specify the output path of the .o file generated from .cpp file and put .o file in the same folder of its .cpp file, like:
TEST
|-- DIR1
| |-- TEST1.cpp
| |-- TEST1.o
| `-- TEST1.h
|-- DIR2
| |-- TEST2.cpp
| |-- TEST2.o
| `-- TEST2.h
|-- Makefile
|-- TEST
|-- TEST.pro
|-- main.cpp
`-- main.o
qmake puts all object files in one directory and you cant change it. But you can add this line to your .pro file
OBJECTS_DIR = .obj
Then it will create a directory named obj and put all object files there.
In Zend Framework 1.8, using Zend_Tool, the framework now generates controllers, actions, etc. automatically.
What it does is that it also creates a tests directory when creating a project.
quickstart
|-- application
| |-- Bootstrap.php
| |-- configs
| | `-- application.ini
| |-- controllers
| | |-- ErrorController.php
| | `-- IndexController.php
| |-- models
| `-- views
| |-- helpers
| `-- scripts
| |-- error
| | `-- error.phtml
| `-- index
| `-- index.phtml
|-- library
|-- public
| `-- index.php
`-- tests
|-- application
| `-- bootstrap.php
|-- library
| `-- bootstrap.php
`-- phpunit.xml
So what does it do? How do I actually use it?
I am reading about unit-testing and hopefully this will help understand it more.
Thanks,
Wenbert
You have to create the test folder manually. I followed this tutorial.