From 888543fa86f43b18070c31ccb408b14d856f9483 Mon Sep 17 00:00:00 2001 From: REYNEP Date: Sun, 6 Apr 2025 00:04:00 +0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8A=20C++=20Linking=20&=20=F0=9F=9B=A0?= =?UTF-8?q?=EF=B8=8F=20Building=20on=20=F0=9F=AA=9F=20Windows=20using=20cl?= =?UTF-8?q?ang-MSVC-toolchain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 31 +++++++++++++--- src/cs50.h | 8 ++++ 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f16c55e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,99 @@ +#[[ ********** Boost Software License - Version 1.0 - August 17th, 2003 ********** +Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompa +nying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transm +it the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is fu +rnished to do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including the above license grant, this restriction and +the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works +of the Software, unless such copies or derivative works are solely in the form of machine-executable object code genera +ted by a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WAR +RANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGH +T HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# *************** END Boost Software License BLOCK *************** ]] +# +# *************** This code is part of REY_FetchV4:- *************** ]] +# *************** The Original Code is Copyright (C) 2025, REYNEP *************** ]] +# *************** https://github.com/REYNEP/REY_LoggerNUtils/tree/main/REY_FetchV4#readme *************** ]] + + +#[[ + USAGE:- + [TERMINAL] + git clone https://github.com/cs50/libcs50 + [CMAKE] + add_subdirectory("path/to/libcs50") + target_link_libraries (YOUR_APP_NAME PUBLIC cs50) + + CMake Introduction:- + https://www.youtube.com/playlist?list=PLK6MXr8gasrGmIiSuVQXpfFuE1uPT615s + watch first 6/7 videos, these are pretty short + + CMake is just way too powerfull 😉 +]] + + # --Error if not GCC/clang-- + if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")) + message(STATUS "[REY_FetchV4.libcs50.cmake]") + message(STATUS "FATAL_ERROR:- ") + message(STATUS " libcs50 requires GCC or Clang. Your compiler is: ${CMAKE_CXX_COMPILER_ID}") + message(STATUS " However tho:- On windows you can use [Visual Studio Build Tools] 'clang-MSVC-toolchain'") + message(FATAL_ERROR "(Read Message Above)") + endif() + + + cmake_minimum_required(VERSION 3.25 FATAL_ERROR) + project("cs50_PROJECT" VERSION 0.1) + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + + + #set(cs50_SOURCE_DIR "${REY_FETCH_${TN}_BASE_DIR}/${Git_CloneDir_Name}") + set(cs50_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") + + # --------src--------- + set(SRC + ${cs50_SOURCE_DIR}/src/cs50.c + ) + + message(STATUS "SRC:- ${cs50_SOURCE_DIR}/src/cs50.c") + + set(INC + ${cs50_SOURCE_DIR}/src + ) + # -------------------- + + # --------lib--------- + add_library (cs50 ${SRC}) + target_include_directories (cs50 PUBLIC ${INC}) + set_target_properties (cs50 PROPERTIES OUTPUT_NAME "libcs50" PREFIX "") + set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/.install CACHE PATH "" FORCE) + + + + # ------warning------- + if (WIN32 OR MSVC) + # Enable -Wdeprecated-declarations for GCC and Clang + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + message(STATUS "[REY_FetchV4.libcs50.cmake] CLANG / GNU compiler") + target_compile_options(cs50 PRIVATE -Wdeprecated-declarations) + target_compile_definitions(cs50 PRIVATE _CRT_SECURE_NO_WARNINGS) + endif() + endif() + # -----suppressed----- + + + + # ------install------- + install(TARGETS cs50 + DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.install) + # Same Destination is used for installing "fmt::fmt" --> Check: `REY_FetchV4.fmt.cmake` + + # Install header files + install(FILES + ${cs50_SOURCE_DIR}/src/cs50.h + DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.install) + # -------------------- \ No newline at end of file diff --git a/README.md b/README.md index 3f9e611..b3f6835 100644 --- a/README.md +++ b/README.md @@ -29,19 +29,40 @@ $ yum install libcs50 ### From Source (Linux and Mac) 1. Download the latest release from https://github.com/cs50/libcs50/releases -1. Extract `libcs50-*.*` -1. `cd libcs50-*` -1. `sudo make install` +2. Extract `libcs50-*.*` +3. `cd libcs50-*` +4. `sudo make install` By default, we install to `/usr/local`. If you'd like to change the installation location, run `sudo DESTDIR=/path/to/install make install` as desired. +### From Source (Windows / CMake) [using clang-MSVC-toolchain] +1. Download Visual Studio Build Tools + - https://github.com/bycloudai/InstallVSBuildToolsWindows + - Remember to enable C++ Clang Tools inside _**"Desktop Development with C++"**_ + +2. USAGE:- + - [TERMINAL] + - `git clone ` + - [CMAKE] + - `add_subdirectory("path/to/libcs50")` + - `target_link_libraries(YOUR_APP_NAME PUBLIC cs50)` + - [VSCode] + - C/C++ Extensions Pack with CMAKE Tools [`ms-vscode.cmake-tools`] + - `CMake: Select a Kit:- Clang` + - for other IDEs, search up "how to use MSVC Clang Toolchain on windows" + +3. CMake Introduction:- + - https://www.youtube.com/playlist?list=PLK6MXr8gasrGmIiSuVQXpfFuE1uPT615s + - watch first 6/7 videos, these are pretty short + - CMake is just way too powerfull 😉 + ## Troubleshooting 1. If, when compiling a program, you see `/usr/bin/ld: cannot find -lcs50`: Add `export LIBRARY_PATH=/usr/local/lib` to your `.bashrc`. -1. If, when compiling a program, you see `fatal error: 'cs50.h' file not found`: +2. If, when compiling a program, you see `fatal error: 'cs50.h' file not found`: Add `export C_INCLUDE_PATH=/usr/local/include` to your `.bashrc`. -1. If, when executing a program, you see `error while loading shared libraries: libcs50.so.8: cannot open shared object file: No such file or directory`: +3. If, when executing a program, you see `error while loading shared libraries: libcs50.so.8: cannot open shared object file: No such file or directory`: Add `export LD_LIBRARY_PATH=/usr/local/lib` to your `.bashrc`. Close and reopen any terminal windows. diff --git a/src/cs50.h b/src/cs50.h index fc58db5..2d75a03 100644 --- a/src/cs50.h +++ b/src/cs50.h @@ -50,6 +50,10 @@ */ typedef char *string; +#ifdef __cplusplus +extern "C" { +#endif + /** * Prompts user for a line of text from standard input and returns the * equivalent char; if text is not a single char, user is prompted @@ -108,4 +112,8 @@ long long get_long_long(const char *format, ...) __attribute__((format(printf, 1 string get_string(va_list *args, const char *format, ...) __attribute__((format(printf, 2, 3))); #define get_string(...) get_string(NULL, __VA_ARGS__) +#ifdef __cplusplus +} #endif + +#endif // CS50_H