How to do profiling a C++ project with gprof? - c++

I have a big C++ program in VS code under Ubuntu 18.4, I am using CMake. I need to Know what areas are spending more time and memory space. I am a beginner in coding in Linux and using CMakeLists.txt files. I tried to use gprof, I added
if(MSVC AND MT)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
endif()
to my CMakeLists.txt file. Then I complied the program using cmake .. and then make, after that I run the program with an example, and when using the command gprof ./myexe I get gmon.out: No such file or directory

Related

How to use CMake to compile *.cpp and *.cu seperately?

I want to implement a Matrix class, and I want to use CUDA to speed up matrix multiplication. But when I try to compile the source files. I meet some problems. I know that I need to use NVCC to compile the *.cu. But I don't know how to write the CMakeLists.txt to compile the *.cu and *.cpp at the same time. I've tried many ways I found on Google, but they all don't work. Here is my current CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Project_4 LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_BUILD_TYPE RELEASE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
find_package(CUDA REQUIRED)
include(FindCUDA)
include_directories(${CUDA_INCLUDE_DIRS})
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
include_directories(include)
aux_source_directory(src DIR_SRCS)
file(GLOB cu *.cu)
cuda_add_executable(cuda ${DIR_SRCS} ${cu})
add_executable(Project_4 ${DIR_SRCS})
set_target_properties(Project_4 PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
Then when compiling, it will report that CMake Error at cuda_generated_ cuda_helper.cu.o.RELEASE.cmake:282. And if I write CMakeLists.txt in this way:
cmake_minimum_required(VERSION 3.16)
project(Project_4 LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_BUILD_TYPE RELEASE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
include_directories(include)
aux_source_directory(src DIR_SRCS)
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
add_executable(Project_4 ${DIR_SRCS})
set_target_properties(Project_4 PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
Then it will report error error: expected primary-expression before "<" token at where I call the kernel function.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. I will appreciate a lot. Thanks.
I created an example for you. It is not based on your cmake. But it works for me maybe it helps you.
clone repo: git clone -b cmake_cuda_example https://github.com/werto87/durak_computer_controlled_opponent.git
install: conan dependency manager
Install: cuda toolkit
create an empty folder called build inside the "durak_computer_controlled_opponent" folder
go inside this folder and inside the terminal run "conan install .. --build missing"
cmake ..
cmake --build .

Cross-compile SDL2 from linux to Windows doesn't make an exe

I am using a WSL running Ubuntu and am trying to get an SDL2 program to compile using the Ubuntu and then run the program on Windows. I have been able to get it to build, however it doesn't make an exe (and changing the file extension to exe doesn't fix it). Also even though I set the CMAKE_CXX_FLAGS to have -o Crawl_The_Dungeon.exe the file still ends up being named all lower case like the project name. I was able to get this to compile and run on linux when I still had a linux machine.
I am still pretty new to CMake so I don't fully understand if I am close at all or not at all.
SET(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${crawl_the_dungeon_SOURCE_DIR}/CMakePath")
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
project(crawl_the_dungeon)
set(SOURCE_FILES final.cpp Soldier.cpp TilesEnum.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --static -std=c++0x")
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(crawl_the_dungeon ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc")
EDIT: I was dumb to include -o Crawl_The_Dungeon.exe as it now creates an exe. However it says that I can't run the program when opening it.

Visual Studio Code debugger doesn't show the contents of variables

I'm using Visual Studio Code on Ubuntu 18.04 to write some C++ code using external libraries. I'm not sure why, but whenever I run the debugger, the contents of most variables doesn't show.
I've attached an example of this issue.
Why is this happening?
Edit:
cmake_minimum_required(VERSION 3.10.2)
project(Proj)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(ARENA_DIR "~/Arena")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CC "g++")
set(CFLAGS "-Wall -g -O2 -std=c++14 -Wno-unknown-pragmas")
add_definitions(${CFLAGS})
set(RM "rm -f")
include_directories(${ARENA_DIR}include/Arena)
include_directories(${ARENA_DIR}include/GenTL)
include_directories(${ARENA_DIR}include/Save)
include_directories(${ARENA_DIR}GenICam/library/CPP/include)
...
set(ARENA_DEBUG_LIBS
${ARENA_DIR}libarenad.so
${ARENA_DIR}libsaved.so
${ARENA_DIR}libgentld.so)
set(ARENA_RELEASE_LIBS
${ARENA_DIR}libarena.so
${ARENA_DIR}libsave.so
${ARENA_DIR}libgentl.so)
set(LIBS
${GENICAMLIBS}
${FFMPEGLIBS}
"/usr/lib/x86_64-linux-gnu/libpthread.so")
add_executable(main main.cpp)
set_target_properties(main PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
target_link_libraries(main ${LIBS} debug ${ARENA_DEBUG_LIBS})
target_link_libraries(main ${LIBS} optimized ${ARENA_RELEASE_LIBS})
Edit 2:
After changing the g++ flag to -O0 instead of -O2, I get this result:
But I would expect to be able to see the object's properties.

cross compile dll on mac OSX with cmake and mingw

I installed the bwapi via wine on Mac OSX. And I try to compile the ExampleAIModule in it with CMake and MinGW on Mac OSX. The ExampleAIModule is actually a VS2017 project, I write a CMakeLists.txt, ran cmake CMakeLists.txt && make VERBOSE=1 got some errors. I want to know how to fix my CMakeLists.txt?
The folder structure:
BWAPI/ExampleAIModule/CMakeLists.txt
BWAPI/ExampleAIModule/Source/*.cpp *.h
BWAPI/include/*.h
BWAPI/lib/BWAPI.lib
BWAPI/lib/BWAPIClient.lib
make got errors:
Linking CXX shared library libExampleAIModule.dylib
/usr/local/Cellar/cmake/3.2.2/bin/cmake -E cmake_link_script CMakeFiles/ExampleAIModule.dir/link.txt --verbose=1
i686-w64-mingw32-g++ -std=c++11 -O3 -DNDEBUG -dynamiclib -Wl,-headerpad_max_install_names -o libExampleAIModule.dylib -install_name #rpath/libExampleAIModule.dylib CMakeFiles/ExampleAIModule.dir/Source/Dll.cpp.o CMakeFiles/ExampleAIModule.dir/Source/ExampleAIModule.cpp.o -L/.wine/drive_c/Starcraft/BWAPI/ExampleAIModule/../lib
i686-w64-mingw32-g++: error: rpath/libExampleAIModule.dylib: No such file or directory
i686-w64-mingw32-g++: error: unrecognized command line option ‘-install_name’
make[2]: *** [libExampleAIModule.dylib] Error 1
make[1]: *** [CMakeFiles/ExampleAIModule.dir/all] Error 2
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
################### Variables. ####################
# Change if you want modify path or other values. #
###################################################
set(PROJECT_NAME ExampleAIModule)
# Output Variables
# Folders files
set(CPP_DIR_1 Source)
set(HEADER_DIR_1 Source)
############## Define Project. ###############
# ---- This the main options of project ---- #
##############################################
project(${PROJECT_NAME} CXX)
# Define Release by default.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "Build type not specified: defaulting to release.")
endif(NOT CMAKE_BUILD_TYPE)
# Definition of Macros
add_definitions(
-DNOMINMAX
-D_DEBUG
-D_WINDOWS
-D_USRDLL
-DEXAMPLEAIMODULE_EXPORTS
-DUNICODE
-D_UNICODE
)
################# Flags ################
# Defines Flags for Windows and Linux. #
########################################
if(MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W3 /EHsc")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W3 /EHsc")
endif(MSVC)
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
endif(NOT MSVC)
########### Add by me #############
SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
include_directories(../include)
link_directories(../lib)
################ Files ################
# -- Add files to project. -- #
#######################################
file(GLOB SRC_FILES
${CPP_DIR_1}/*.cpp
${HEADER_DIR_1}/*.h
)
# Add library to build.
add_library(${PROJECT_NAME} SHARED
${SRC_FILES}
)
CMake always use toolchains to compile for a specific platform/target, as mentioned here. However, for cross-compilation, you need to specify the toolchain file it needs to use. This is done using the CMAKE_TOOLCHAIN_FILE variable, as explained here.
The parameters related to the compilation (such as the system name, processor, compiler, etc.) on the target platform shouldn't be in the CMakeLists.txt file where you defined your project (using the project function).
Instead, you're supposed to set the compilers and other parameters related to cross-compilation in a toolchain file. And you may have a toolchain file for each platform you like to cross-compile for.
This is how your toolchain file may look like, assuming that the compilers are in the path:
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
If your toolchain file has the path /toolchains/windows.cmake, you need to pass -DCMAKE_TOOLCHAIN_FILE=/toolchains/windows.cmake to cmake.
This design keeps your project's CMakeLists.txt file toolchain-agnostic.

JetBrains CLion can not suggest member methods of string

I'm new to JetBrains' CLion. Sorry if my question is basic but I didn't find any answer by searching.
#include <string>
int main() {
std::string str;
str.assign("ABC");
}
This very simple code compiles fine but the problem is the editor can't suggest member methods for str.
I'm using CLion 2016.3.2 on Fedora 25
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(/usr/include /usr/local/include /usr/local/pgsql/include)
set(SOURCE_FILES
main.cpp
commander.cpp
)
add_custom_target(ITP command make -C /home/ben/projects/ITP)
Note: when I point to #include the editor didn't knows relevant include file!
Finally I reached to an answer!
Clion works with Cmake. there for CMakeLists.txt is very important.
many settings with cmake can effect code compilation, build types and ...
for more details please see my new CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
set(SOURCE_FILES
main.cpp
commander.cpp)
link_directories(/usr/local/pgsql/lib/)
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
message("C Flags = ${CMAKE_CXX_FLAGS_DEBUG}")
add_executable(itp OrderManagementSystem/main.cpp)
add_custom_command(
TARGET itp POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/config.ini
${CMAKE_CURRENT_BINARY_DIR})
TARGET_LINK_LIBRARIES( itp
pthread
boost_system boost_thread
protobuf
zmq
pq
)
I'm new with both Clion and cmake. but these two ..