Cpp in visual studio: cmake and being able to compile

  • Context: C/C++ 
  • Thread starter Thread starter cppIStough
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on compiling a C++ project using CMake in Visual Studio, specifically for the file run_logger.cpp. The user provided a directory structure and a CMakeLists.txt file, which specifies a minimum CMake version of 3.10 and includes source files from the "source" and "include" directories. The issue arises from the incorrect specification of the executable target in the CMakeLists.txt, where the filename should not include the .cpp extension. The correct command is to use add_executable(run_logger ${SOURCES}) instead.

PREREQUISITES
  • Familiarity with CMake version 3.10 or higher
  • Understanding of C++ project structure and file organization
  • Basic knowledge of Visual Studio IDE
  • Experience with compiling C++ code
NEXT STEPS
  • Review CMake documentation on add_executable syntax
  • Explore CMake file(GLOB_RECURSE) for managing source files
  • Learn about Visual Studio project settings for CMake integration
  • Investigate common CMake errors and troubleshooting techniques
USEFUL FOR

C++ developers, software engineers, and anyone using CMake with Visual Studio for building and managing C++ projects.

cppIStough
Messages
24
Reaction score
2
I have the following directory of c++ files, and it looks like:

d.lib
--apps
----run_logger.cpp
----CMakeLists.txt
--include
----logger.h
--source
----logger.cpp

I want to build run_logger.cpp. I'm using Visual Studio. My CMakeLists is
Code:
cmake_minimum_required(VERSION 3.10)

project(d_lib)

# Add your source files
file(GLOB_RECURSE SOURCES "source/*.cpp" "include/*.h")

# Add your executable
add_executable(run_logger.cpp ${SOURCES})

How can I get this to compile? Thanks for your help.[/code]
 
Last edited:

Similar threads

  • · Replies 12 ·
Replies
12
Views
4K
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 29 ·
Replies
29
Views
3K
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K