Changing Compiler Flags for One Project in CMakeLists.txt - visual-studio

I have a CMakeLists.txt that specifies multiple executables. For only one of these projects, I'm wanting to use the Static Runtime.
I found this solution here: Setting the MSVC runtime in CMake
This involves changed the CMAKE_CXX_FLAGS and CMAKE_C_FLAGS (as well as others).
However, doing something like this will change the runtime library for every project. With my testing, doesn't matter where you set these, it changes it for everything.
Is there any way to do this for just one project?

You'd be able to do this if you give your "Static Runtime" executable its own CMakeLists.txt file and include it from the parent one via add_subdirectory.
Variables set in the subdirectory CMakeLists.txt don't affect similar ones in the parent scope, so you can have the /MD or /MDd flags in the parent CMAKE_<LANG>_FLAGS_<BUILD>, and replace these with /MT or /MTd in the subdirectory.
Note that even though the command is called add_subdirectory, it need not refer to an actual subdirectory in the filesystem sense - just some other directory with its own CMakeLists.txt.

Related

cmake + qt + visual studio: moc objects on build

I am using cmake + qt + visual studio to work on a project. Problem I am having it that I would like visual studio to create new moc objects if I modify the QT ui files. If I just do a full build everything works file, but if I just modify something on the ui file it does not "auto moc" and I have to rebuild the whole project.
The cmake file I have is pretty simple:
cmake_minimum_required(VERSION 3.2)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(main)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Widgets)
file(GLOB CPP_FILES *.cpp)
add_executable(main ${CPP_FILES})
target_link_libraries(main Qt5::Widgets)
target_compile_features(main PUBLIC cxx_nullptr)
Does anyone know a way to get this to work (having visual studio to detect ui file modifications and "auto moc" the modified ui file)?
Start by replacing your file(GLOB ...) with explicitly listing out the files you want to include if you want proper dependency handling. This will also ensure the build is creating dependencies for the set of files you are expecting it to. This answer has more details about why you probably want to do this, aside from the reasons below.
The CMake documentation for AUTOUIC includes this statement:
If a preprocessor #include directive is found which matches
ui_<basename>.h, and a <basename>.ui file exists, then uic will be
executed to generate the appropriate file.
Can you confirm that your .cpp sources have #include directives that follow this pattern? In your file(GLOB ...) you are only capturing the .cpp files and not the .h files, so if you've only got the #include directives in the headers, AUTOUIC may not pick them up properly. It's been a while since I've used this and I can't recall if AUTOUIC would still find them if you only list the .cpp files and not the headers too in your add_executable() call, but it's something for you to try. You also may be facing a similar situation with AUTOMOC if you have headers which use the Q_OBJECT and Q_GADGET macros. So just explicitly list out your .cpp and .h files you give to add_executable() and see if that addresses your problem.

Add directory containing pre compiled module in CMakeLists

I am modifying a CMakeLists.txt file for a fortran code. I need to add a directory that contains a precompiled module.
How can I do that in CMakeLists.txt ?
There is a target property called Fortran_MODULE_DIRECTORY
For good measure, I also add the module directory as an include directory. gfortran looks for .mod files in the include directories.
set_target_properties( Target PROPERTIES Fortran_MODULE_DIRECTORY "dir" )
target_include_directories(Target PUBLIC "dir" )
Edit
Reading through your question again, I see that the module you are interested in is already compiled. Fortran_MODULE_DIRECTORY specifies where to PUT .mod files, and adds that directory as a place to look for them. target_include_directories statement just specifies where to look for them. I am most familiar with using gfortran, which has this in it's man page:
-Idir
These affect interpretation of the "INCLUDE" directive (as well as of the "#include" directive of the
cpp preprocessor).
Also note that the general behavior of -I and "INCLUDE" is pretty much the same as of -I with
"#include" in the cpp preprocessor, with regard to looking for header.gcc files and other such things.
This path is also used to search for .mod files when previously compiled modules are required by a
"USE" statement.
-Jdir
This option specifies where to put .mod files for compiled modules. It is also added to the list of
directories to searched by an "USE" statement.
The default is the current directory.
This is what CMake will do for you and your compiler; you shouldn't need to specify these flags explicitly. In your case, just the includes should be sufficient, since the module has already been compiled.
However, if you find it's not working with your compiler, you may have to specify them manually by setting the variable CMAKE_Fortran_FLAGS.
Hope this helps.

check what run-time static library or dll uses

is there a tool in windows SDK to ckeck what CRT a library uses?
for example I have a *.lib file, how do check if it's compiled with /MDd flag or /MT?
also how to check the same for dll or exe?
can this be done with dumpbin?
If it is a .lib file, a static link library, then you don't know anything about the CRT yet. It wasn't linked yet. You can find out about the original programmer's intention, use a hex viewer to look the .lib file, Notepad will do fine as well. You'll see the original command line that was used to compile the .obj files that were embedded in the .lib file. Simply search for "cl.exe", you'll have a good idea what compiler version was used from the path to cl.exe. And you can see the command line options so you'll know if /MD or /MT was in effect. And the /O option, important to have an idea whether you've got a Debug or Release build.
If it is a .dll file then dumpbin.exe /imports is your best choice. The dependency on the msvcrxxx.dll file will be visible, with xxx the version number like "120". If you see it then the name tells you if /MD or /MDd was used, "d" is appended for the Debug version of the CRT If it is missing then you know that /MT or /MTd was used, no hint about the build flavor available.
Following the recommendations from the library owner is always best, you can get into a lot of trouble when the CRT version or build settings of the library doesn't match yours. With non-zero odds that you have to ask him for an update, YMMV.

Linking to Boost libraries fails because of lib prefix

I installed boost v1.47 DLL (MT, Debug) for VC9 from boostpro. My code #include's <boost/regex.hpp>. When I try to build, VC gives me LNK1104 cannot open file libboost_regex-vc90-mt-gd-1_47.lib, even though boost_1_47/lib has been added to Additional Library Dependencies (in double quotes). The problem seems to be that the name of the library in the directory is not libboost_regex-vc90-mt-gd-1_47.lib, but simply boost_regex-vc90-mt-gd-1_47.lib.
How do I get VC to omit the leading 'lib' ? Any help would be greatly appreciated.
Thanks in advance
In Boost the lib prefix denotes whether the libraries were built for static or dynamic (shared) linking. To indicate you want to dynamically link the libraries (as the name without the lib prefix implies) define the preprocessor directive of BOOST_ALL_DYN_LINK.
In Visual Studio, go to the project's settings, and select Configuration Properties > C/C++ > Preprocessor page. Add BOOST_ALL_DYN_LINK to Preprocessor Definitions.
See also this related question: https://stackoverflow.com/a/2521866/483776

CMAKE: Build library and link against it

I'm trying to use cmake (on Linux with GNU make and g++) to build a project with two sub-directories: MyLib and MyApp. MyLib contains source for a static library; MyApp needs to link against that library. I'm trying to build on Linux with generated makefiles using the following CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (MyProj)
include_directories (MyLib)
file(GLOB MyLibSrc MyLib/*.cpp)
add_library(MyLibrary STATIC ${MyLibSrc})
file(GLOB MyAppSrc MyApp/*.cpp)
add_executable(MyApplication ${MyAppSrc})
target_link_libraries(MyApplication MyLibrary)
This 'almost' works. It fails at link time because while it generates libMyLibrary.a - it is in the root. When I add:
link_directories(${MyProj_BINARY_DIR})
it makes no difference.
I've got a few (inter-linked) questions:
What's the best way to coerce cmake into building my library and executable into a 'staging directory' — say MyStage — to keep targets separate from source?
How do I convince cmake to link the application against the library?
If I wanted to build a debug and a release version, what's the best way to extend my cmake scripts to do this — making sure that the debug application links against the debug library and the release application against the release library?
I'm a relative newcomer to cmake. I've read what I can find on the web, but find myself struggling to get my library to link with my executable. This sort of a configuration, to my mind, should be quite common. An example from which to crib would be very helpful, but I've not found one.
Well, it is better to read this example and do exactly as suggested.
cmake_minimum_required (VERSION 2.6)
project (MyProj CXX)
add_subdirectory(MyLib)
add_subdirectory(MyApp)
Then for each subdirectory specified, CMakeLists.txt files are created
MyLib\CMakeLists.txt
file(GLOB SRC_FILES *.cpp)
add_library(MyLib ${SRC_FILES})
MyApp\CMakeLists.txt
file(GLOB SRC_FILES *.cpp)
add_executable(MyApp ${SRC_FILES})
target_link_libraries(MyApp MyLib)
Use "out of the source build". Make a directory used only for build and while in it, call
cmake <path to the sources, it may be relative>
Either use
link_directories(${MyProj_BINARY_DIR}/MyLib)
or make CMakeLists.txt in each subdirectory - that would be better for project larger than very small.
This is a bit tricky, check out CMAKE_BUILD_TYPE in the docs (you can set it and/or "if" by it). You can also set it from command line:
cmake -DCMAKE_BUILD_TYPE=Debug
I've discovered the 'optimal' solution to (1)... so, thought I should post it here:
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY MyStage)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY MyStage)
The thing that confused me previously is that static libraries are not considered a LIBRARY by Cmake - they're considered to be ARCHIVEs.
Do not add libraries and executables in the root Cmakelists.txt. Add these libraries and executables in Cmakelists.txt of subdirectories.

Resources