I am trying to create a sample project for myself using Plog library. according to its documentation, I must add its include folder to my include path. SO, I add these Lines to C/C++ configuration in my vs-code:
${default}
${workspaceFolder}/**
and this is my main function:
#include <iostream>
#include "plog/Log.h"
int main() {
plog::init(plog::debug, "log.txt");
std::string name;
std::cin >> name;
LOGD << "user entered name :" << name;
std::cout << name << std::endl;
return 0;
}
but when I run this code I am getting this error:
fatal error: plog/Logger.h: No such file or directory
which plog/Logger.h is referenced by log.h in the main function.
plog folder which contains All headers of plog library is located in my project root folder.
this is My folder structure:
root
|_plog
| |
| |_ all my header files *.h
|_main.cpp
Is there any more configuration which I missed? or did I make a mistake in any step?
Try
#include <plog/Log.h>
Why do you have ** after ${workspaceFolder}?
First we need to know what the directory structure is. For instance
+-- C:\
+-- ProgramData
+-- plog-dist
+-- plog
+-- Log.h
+-- lib
+-- plog.lib
+-- bin
+-- plog.dll
+-- Users
+-- Me
+-- Documents
+-- myproject
Let us assume that you already have an environment variable setup
setx PLOG=C:\ProgramData\plog-dist
You can either manage this under property pages or you can set it on individual projects. Let us just do the second method because it is easier and has less explaining. Under C++ General Properties, the first line says Additional Include Directories. Add in
$(PLOG)
This will pick up your environment variable PLOG. In your code
#include "plog/Log.h"
VS will look in $(PLOG) for plog/Log.h. If the distribution is
+-- ProgramData
+-- plog
+-- Log.h
And the environment variable is
setx PLOG=C:\ProgramData\plog
Then you should just
#include "Log.h"
VS will look in $(PLOG) for Log.h
Next go to the linker section of properties. Here, you will see Additional Library Directories. Add $(PLOG)/lib. This is where it can find plog.lib. If plog.lib lives in $(PLOG) then add $(PLOG). It really depends on what the directory structure is.
Next go to the Input property page. Under Additional Dependencies add plog.lib.
If you set up the PLOG variable and type tree /f %PLOG% from the cmd line, it will tell you where everything is relative to $(PLOG).
Assuming that below is your folder hierarchy:
root
|_plog
| |
| |_ Init.h
| |_ Log.h
| |_ Init.h
| |_ Other plog's *.h files
|_main.cpp
In that case, adding ${workspaceFolder} to your include paths must suffice.
When you do #include "plog/Log.h", it expects the plog folder to be a direct children of the include paths. As plog folder is a direct child of ${workspaceFolder} directory, this should work.
Related
I have a folder inside the static directory of an app in a Django project. Lets call this special_folder. It contains some static files that I do not want to commit to Git. The project directory looks something like this:
myproject
|
+-- manage.py
|
+-- .gitignore
|
+-- myapp
|
+-- static
|
+-- special_folder
Now, if I want to ignore this folder for Git, do I need to add something like the following line to the .gitignore file:
myapp/static/special_folder/
Is this the right approach? Or should I just add special_folder/? Thanks for any help.
All paths are relative to the .gitignore.
Based on your folder structure and location of .gitignore file you are correct. The below should work.
myapp/static/special_folder/
Refer Pattern Format.
HTH
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've started a new c++ project , and I am confused with all the CMake capabilities. I have tried to understand better by looking at examples and CMake tutorials
I should create a new project composed of:
Library: It contains some common classes that will be used by the following module(s) (e.g., vector, matrix, image, etc..)
Module (possibly more than 1 in the future): It contains some module-specific classes (e.g., classifier, estimator, etc.) and a main.
My proposed folder structure is as below:
|-- Root Project
|-- CMakeLists.txt
|
|-- Library
| |-- CMakeLists.txt
| |-- include
| | |-- CMakeLists.txt (?)
| | `-- Lib_Class.h
| `-- src
| |-- CMakeLists.txt (?)
| `-- Lib_Class.h
|
|-- Application 1
| |-- CMakeLists.txt
| |-- include
| | |-- CMakeLists.txt (?)
| | `-- Method.h
| `-- src
| |-- CMakeLists.txt (?)
| |-- Method.cpp
| `-- main.cpp
|
|-- Application 2
| |-- CMakeLists.txt
| |
`
The problem arises when I have to actually add the code to the different CMakeLists.txt files. According to my reasoning, I would have:
Root/CMakeLists.txt: For creating the project and adding the subdirectories of the Library and the Module(s).
Library/CMakeLists.txt: This creates the library with the header (from include folder) and source (from src folder) files.
Module/CMakeLists.txt: This creates an executable from the src/main.cpp file using the Library and the module-specific classes with header files in include folder and source files in src folder.
I have 2 questions:
First, I also found solutions in other replies with CMakeLists.txt files in the Library/src and Module/src folders. But I really don't understand how to use them and what to write inside them, because I would have used only the CMakeLists.txt file in the parent folder.
Second, in case I want to link an external library (e.g., OpenCV or dlib) should I link it in the modules and library, individually, or should I link it in the root CMakeLists.txt file (provided that the library is used everywhere)?
I really need some assistance to try to understand CMAKE. Can someone explain or please direct me to a suitable tutorial on this subject.
Matthieu, thank you very much for your help. According to the explanation you provided me, I came out with the following CMakeLists.txt files:
Root/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Project_Name)
add_subdirectory(Library)
add_subdirectory(Application)
Library/CMakeLists.txt
project(Library)
set(LIB_HEADERS
include/Lib_Class.h
)
set(LIB_SOURCES
src/Lib_Class.cpp
)
add_library(Library_Name SHARED ${LIB_SOURCES} ${LIB_HEADERS})
Application/CMakeLists.txt
project(Application)
set(APP_HEADERS
include/Method.h
)
set(APP_SOURCES
src/Method.cpp
src/main.cpp
)
add_executable(Application_Name ${APP_SOURCES} ${APP_HEADERS})
target_link_libraries(Application_Name Library_Name)
Now everything seems to work grat! Thank you again and sorry again for being confusing somethimes!
The root cmakelists should set up all the variables, checking compiler support and library presence.
Then you go to each subfolder and create the libraries and executables based on the source code and the detected libraries. You should also set up all the linked libraries there.
Then cmake will figure out what depends on what.
I am working on a c++ project using cmake that uses hiredis. The CMake and compilation process do not give any errors. However, when I try to execute my project (from the terminal or from the IDE I'm using [CLion], I get the following error:
dyld: Library not loaded: libhiredis.0.13.dylib
Referenced from: /Users/connorriley/CLionProjects/DispatchingOptimization/bin/dispatch
Reason: image not found
I'm not sure why my project is looking for libhiredis.0.13.dylib because the only hiredis library file I have is libhiredis.dylib.
My project file structure is the following:
.
+-- bin
| +-- dispatch (my executable)
+-- lib
| +-- hiredis
| | +-- libhiredis.dylib
| +-- otherlibs
+-- src
| +-- source code/subfolders with source code
additional info:
compiler: clang
os: macOS 10.12.3
cmake version 3.7.2
Looks like your DYLD_LIBRARY_PATH is not set correctly. You can get more information by setting DYLD_PRINT_LIBRARIES and/or some other environment variables mentioned here
But probably you just need to add your hiredis directory to CMAKE_LIBRARY_PATH like this:
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${PROJECT_SOURCE_DIR}/lib/hiredis)
I fixed my issue, it was that I went into my hiredis directory and typed:
make
but didn't follow that with
make install
Therefore, the file that my code was looking for was not in my /usr/local/lib
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)