I have built a project using cmake and some libraries.I want however to add some header and .cpp files in the project which I am going to code.What is the easiest way to do it?Can I just create a .cpp and header files and then build again project in Visual Studio? Or due to the fact that project was built using cmake I can't?
You can put all header/source files in the same folder and use something like
file(GLOB SOURCES
header-folder/*.h
source-folder/*.cpp
)
add_executable(yourProj ${SOURCES})
In this way, you can do either of the following two methods to add new added header/source into VS:
need to generate in CMake again.
fake to edit the CMakeLists.txt a little bit, e.g. simply add a space. And then build your solution in VS, it will automatically add new header/source files.
you need to add every .h and .cpp file to CMakeList.txt like this:
# Local header files here ONLY
SET(TARGET_H
Header.h
Plugin.h
messagelog.h
win32application.h
timer.h
)
# Local source files here
SET(TARGET_SRC
Plugin.cpp
messagelog.cpp
win32application.cpp
timer.cpp
)
then configure and build the solution again and reload it in VS.
Although it's a late Response and I just saw it. I am using CLion IDE from JetBrains, which adds these header and .cpp files automatically when you create them. Although it may not be your need, it may be useful for other peoples who see it.
Related
Apologies for a very basic question. I am trying to port from a CMake project into a visual studio project.
Basically I want to run the project from visual studio without using the CMake file. In the project I need to port, there are many folders and sub folders that contains many .cpp and .h files. These are included to the main cpp file as using #includes.
My Case
The library I want to include
and my main.cpp
For instance to make things simple assume I have a main file main.cpp and this file includes #include "Libpfs/colorspace/colorspace.h". The Libpfs is a folder and it has many sub folders one of which is colorspace folder and this has many .h and .cpp files. One .h file is colorspace.h that is included in main.cpp using the #include and the folder also has .cpp file i.e. colorspace.cpp.
My Attempt
My objective is include them to my project.
Now here is what I have tried
in Visual Studio Project->properties->C/C++ in Additional Include Directories I gave the path of the folder that contains Libpfs but this approach did not work and gave linker errors this might be because I have no lib files for the Libpfs (correct me if I am wrong). I only have .cpp files of the corresponding .h files. I presume the cause of error is the the .cpp files are not compiled yet.
My Question
How can I include the cpp file to my project as well (not the lib files since I dont have those).
Using this for a source, I find the CMakeLists.txt to contain nothing special.
FILE(GLOB COLORSPACE_H *.h)
FILE(GLOB COLORSPACE_HXX *.hxx)
FILE(GLOB COLORSPACE_CPP *.cpp)
SET(LIBPFS_H ${LIBPFS_H} ${COLORSPACE_H} ${COLORSPACE_HXX} PARENT_SCOPE)
SET(LIBPFS_CPP ${LIBPFS_CPP} ${COLORSPACE_CPP} PARENT_SCOPE)
So you can just add all the files to a VS C++ project. I would use some directory management, to separate these sources from your own.
But anyhow, in that case, you should include the sources by relative path. E.g.
#include "../../Libpfs/colorspace/colorspace.h"
Alternatively, you could put everything in a separate C++ library (static .lib or dynamic .dll). In that case you should but the binaries in in a bin path and add that as additional library directory (project properties of your own project) and put all the header files in an include/Libpfs path and add that as additional include directory. In that case you should include the files as.
#include <Libpfs/colorspace/colorspace.h>
On another topic
#define pow_F(a,b) (xexpf(b*xlogf(a)))
I found this define only in the sources of the same source used above.
It seems to be sourced from sleef and according to this it should give a speedup. But you should measure if that is really still true, instead of doing copy-paste/cargo cult programming.
I think generally you should use the standard library std::pow, which has overloads for float, double and long double. The compiler will in most cases optimize its use for you.
open explorer , look for C:\Program Files\Microsoft SDK\ and then go after dir with alot of .lib's in its \lib .
it's an msvc source for .lib and other stuff like that . copy your library there
I have a project layout as follows:
workspace
project_a
project_a -> .h files here
Root -> .cxx files here
project_b
project_b -> .h files here
Root -> .cxx files here
I cannot change the directory layout due to the build system that we're using.
Headers are included as
#include "project_a/some_header.h
also from the corresponding .cxx file.
I've created a CMakeLists.txt file in the root directory, that adds all my projects via include_directories(project_a project_b), which should be the path prefixed before the one given in the #include. CLion does not manage to find and index any of my files.
Additionally, I have an automatically generated directory of headers of structure
include
lib_a -> .h files
lib_b -> .h files
and I've set them up accordingly, but it also does not work.
Does CLion not manage to resolve the prefixed path in the #include or why is this not working?
In CMakeList.txt, which should be located in parent folder, "workspace" folder in that situation, add
set(INCLUDE_DIRECTORIES ./)
If, for example, there is a parent folder, that holds include files:
workspace
includes_folder
project_a
a.h
b.h:
#include <project_a/a.h>
Then CMakeList.txt should contain
set(INCLUDE_DIRECTORIES ./)
include_directories(includes_folder)
If the only thing that doesn't work is Clion's interpretation of your headers then you should check out the Clion FAQ. If Clion's not working the way you expect then your CMake project might not be set up correctly or you might be doing something unintentionally. You should show us your CMakeLists.txt.
From the Clion FAQ:
Q: CLion fails to find some of my headers. Where does it search for them?
A: CLion searches through the same places CMake does. Set the INCLUDE_DIRECTORIES variable in CMake to provide the headers path to the IDE.
By the way, in this wiki you can find a lot of useful CMake variables with descriptions that can be especially helpful if you are new to CMake.
Q: Refactorings are not working and highlighting is wrong, even though the project can be compiled correctly. What’s happened?
A: Most probably CLion is unaware of some files in your project (these files are grayed out in the project tree):
non-project-files
It gets this information from the CMakeLists.txt files in the following way:
set(SOURCE_FILES main.cpp)
add_executable(my_exec ${SOURCE_FILES})
This is how CLion now knows that main.cpp is included in your project. As for now, header files (in case their names differ from the appropriate .cpp files already added to the SOURCE_FILES variable) should also be included in the project in that way.
I'm trying to build a Makefile with separate folder for my sources and my headers. I have a the root of my project that contain an include folder that holds my .hpp files, and a source folder that holds my .cpp files. How can i build the Makefile that it builds all the .cpp with the respective .hpp files ?
How to create the program output in a folder called build ?
Thank you in advance
Make is so diverse, there's a LOT of ways to do this, but the end result is -I<your hpp path> (or -J) needs to be passed to the compiler to tell it additional include paths to search when resolving #include
The path needs to relative to the invocation of the rule directory, or relative to the file (I'm pretty sure the compiler searches both).
A lot of makefiles use CPPOPTS and CCOPTS or some variant of that in the makefile to pass extra options to the C or CPP compiler. Try adding:
CPPOPTS += -I..\include
To your makefile (assuming you've segregated your source and include files that way).
Again, this is ALL dependent on your makefile.
I'm trying to build a Makefile with separate folder for my sources and my headers.
An alternative way would be to have your headers in the same directory as your .cpp files. And in that top level include folder you can put symbolic links to the headers.
I am using lodepng to produce compressed image files. I have stored the .cpp and .h files (there is only 1 of each) in C:/Program Files/Common Files/lodepng/lodepng, and I want my visual studio project to automatically look in these folders.
I have added C:/Program Files/Common Files/lodepng to my Include Directories, so wherever I have #include <lodepng/lodepng.h> it not picks up that header file, which is nice.
However, when I try to compile I get a linker error (obviously) because I haven't told VS where to look for the .cpp file (it's in C:/Program Files/Common Files/lodepng/lodepng/lodepng.cpp). But how do I tell it that?
Currently the fix I'm using is to simply drag and drop loadpng.cpp into my Source Files of the project, but this isn't a very nice solution.
Any ideas?
You have to add your .cpp files to (compile with) your project.
Right click the project in the solution explorer, then select Add -> existing item and select your .cpp files. Note the folder the files live must be inlcuded to your diretories
VC++ would only look files for you when encountering directives like:
#include "lodepng.h"
Though possible it is not a common practice to include a cpp file, in fact you could include text file for example to include very large data file etc.
Best practice is to add cpp file into your project, don't put any cpp file in a header folder and don't include them unless there are no any alternatives.
I created a class (say, myclass.h/cpp). I want to use the class from many different places. Therefore, I put those files in a folder (say, C:\cpp_include) and I want to include them from whatever folder my codes are. I have a code which uses the class (say, main.cpp). In main.cpp, I include myclass:
#include "myclass.h"
I compile using a .pro file and nmake. In the .pro file, I specify the folder as:
INCLUDEPATH += C:\cpp_include
When I compile the code using nmake, myclass.h is properly included, but myclass.cpp doesn't seem to be found by compiler.
When I specify myclass.cpp as one of the source files in .pro file:
SOURCES += main.cpp C:\cpp_include\myclass.cpp
The exe file is built correctly. But, I would like myclass.cpp file to be found automatically when myclass.h is included, i.e. without setting myclass.cpp as a source file. Would this be possible? It looks like that's what happens with classes from Qt and Qwt (e.g .h/cpp files in /src/ folder in Qt and Qwt). Am I missing somthing?
Thanks a lot!
Daisuke
A simple technique is to have build scripts (makefiles) in the cpp directories. Write a rule that traverses the directories, executing the build scripts. This one step in isolating functionality and also allows one to use libraries.
That's just not how it works. The .cpp is the file that matters, header files (.h) just get copied into the other .cpp files. Therefore you need to add the myclass.cpp to your sources for compiling. Or, if it's a library class, you could also compile it once into a static library (.lib) and just add that to your linker files. But you ultimately need to somehow include you implementation in the project where it's used.