4

Here is recommended to pass CMAKE_BUILD_TYPE as an argument to cmake when I want to obtain debug or release project builds. I'm trying to compile libharu with cmake and I would like to compile it with debug symbols. I've searched CMakeLists.txt included in libharu for following strings:

CMAKE_BUILD_TYPE
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_DEBUG

but I've found nothing. My question is that if it does make any sense to specify CMAKE_BUILD_TYPE when libharu's CMakeLists.txt doesn't mention it? If not, how can I compile libharu with debug symbols?

PS: I've noticed that project that was generated for Visual Studio 2013 with cmake had set Debug/Win32, is this sufficient? Where in CMakeLists.txt is specified this info?

PPS: I guess this question is highly depending on particular project but is there some way to do this in general? I mean, does CMAKE_BUILD_TYPE=Debug always create Debug build or is there something else that I should be aware of?

Thanks

Community
  • 1
  • 1
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • I would say that changing CMAKE_BUILD_TYPE any way except for by passing directly to cmake should be avoided. However, for this library, what happened when you set CMAKE_BUILD_TYPE to Debug? Did it compile with debug symbols? http://stackoverflow.com/questions/1999654/how-can-i-tell-if-a-library-was-compiled-with-g – Robert Prévost Sep 08 '16 at 23:30
  • @RobertPrévost seems that it is already compiled with it, at least pdb file was created. Is there way to check if current build is created with debug symbols? – Wakan Tanka Sep 09 '16 at 09:27
  • There seems to be a few different ways to check how the library was built (e.g., if it was compiled /mt or /mtd) and many questions on SO about that. A pdb file does certainly help debugging. From microsoft: "A program database (.pdb) file, also called a symbol file, maps the identifiers that you create in source files for classes, methods, and other code to the identifiers that are used in the compiled executables of your project." – Robert Prévost Sep 09 '16 at 23:57

1 Answers1

6

Setting configuration type via CMAKE_BUILD_TYPE switches set of additional options for compiler to one, which normally reflects meaning of the configuration. That is, passing

-DCMAKE_BUILD_TYPE=Debug

to cmake tells compiler to generate debugging information unless CMakeLists.txt modifies that behavior.

Config-dependent compiler options are contained in variables CMAKE_<LANG>_FLAGS_<CONFIG>. For example, variable CMAKE_C_FLAGS_DEBUG contains additional options for C compiler in "Debug" configuration. These variables are filled by CMake automatically, and CMakeLists.txt itself rare modifies them.

So, if you found that CMakeLists.txt doesn't play with variables CMAKE_BUILD_TYPE and CMAKE_<LANG>_FLAGS_<CONFIG>, then it follows common conventions about configuration type.


This doesn't mean that CMakeLists.txt shouldn't play with that variables.

Often CMakeLists.txt sets CMAKE_BUILD_TYPE to some default value, provided the variable is not set by the user and single-config generator is used. CMakeLists.txt also may set some of variables CMAKE_<LANG>_FLAGS_<CONFIG>, if default setting for compiler is not suited for the project.

But even if CMakeLists.txt does not touch these variables, they work.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Tank you for answer. If I understand correct then well written `CMakeLists.txt` should not mess with `CMAKE_BUILD_TYPE` and `CMAKE__FLAGS_` and passing those variables via `-D` is correct way? – Wakan Tanka Sep 09 '16 at 09:18
  • `well written CMakeLists.txt should not mess with CMAKE_BUILD_TYPE and CMAKE__FLAGS_` - Not quite true. I mean that event if `CMakeLists.txt` does not touch them, they works in expected manner. See updated answer. – Tsyvarev Sep 09 '16 at 09:54