Skip to content

😊 C++ Linking & πŸ› οΈ Building on πŸͺŸ Windows using clang-MSVC-toolchain #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
# --------------------
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <This Repository URL>`
- [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.
Expand Down
8 changes: 8 additions & 0 deletions src/cs50.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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