Michael De
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: NJIT --- # Setting up a cs280 dev environment ## Linux Users ### Recommended: Install c and c++ compiler and GNU Debugger Debian/Ubuntu/ElementaryOS: ``` sudo apt install gcc g++ gdb -y ``` RedHat/CentOS/Fedora ``` sudo dnf install gcc g++ gdb -y ``` **Note:** You can also use `dnf groupinstall 'Development Tools'` however this will install other tools you may not need for your coursework. For information on using GNU tools see [Using the GNU tools](https://hackmd.io/s/r13syBL74#Using-the-GNU-Tools) **Note:** If your source code has the `.cpp` file extension, `g++` will automatically know to interpret the code as `c++`. If you do not have this file extension please add the arg `-std=c++11` to your command. The `-o` flag precedes the filename you wish to give the compiled program. By default, if no filename is given a file called `a.out` will be created. The `-g` flag compiles your program with debug flags. This is important for when you are using a debugger. For more information see [using GNU Debugger](https://hackmd.io/s/r13syBL74#Debugging-using-GDB). ## MacOS Users ### Recommended: MacOS Users who like Linux The GNU c++ toolchain is available via [homebrew](). ### Install Homebrew Simply run this command: ``` /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ``` You may need to prefix this command with `sudo` if you did not yet install the xcode command line tools. Homebrew will need these tools in order to properly install. Close (`Command+Q`) your Terminal and reopen it. Type `brew` to ensure brew is working. ### Recommended: Install c/c++ compiler and gdb ``` brew install gcc brew install g++ brew install gdb ``` You can now build and debug your code using the standard GNU Toolchain. For more info see [Using the GNU Tools](https://hackmd.io/s/r13syBL74#Using-the-GNU-Tools) **Note:** MacOS comes preinstalled with clang and a different version of gcc. This will cause problems when trying to meet requirements for certain courses (cs288). We use the homebrew versions instead because they are built from the official gnu versions. Because we are replacing the default you will want to add the location of the brew versions of these binaries to your `PATH`. (You can google that or add the commands to this doc). ### MacOS Users who like XCode You can use the XCode compiler without using the XCode IDE if you prefer it to the GNU toolchain. It will require some experience with the bash command line. Install the xcode command line tools by running: ``` xcode-select --install ``` ![XCode install](https://i.stack.imgur.com/nakcs.png) You will be prompted to install the tools, select `Install` to install the command line tools. You can install the IDE later if you wish. #### Compiling with XCode If you are using XCode in command line tools, you can compile your `.cpp` file with the following command: ``` c++ -c prog.cpp -o prog ``` This will use the `clang` compiler to compile your code and output a file named `prog`. #### Debugging with XCode You can debug with `lldb` which is XCodes debugger. For more information look [here](https://developer.apple.com/library/archive/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/lldb-basics.html#//apple_ref/doc/uid/TP40012917-CH2-SW1) ## Windows Users ### Windows Users who know some Linux #### Install WSL If you know some Linux but are a Windows user for whatever reason (bad laptop hardware compatibility) you may have a much better time using WSL for your development work. To get started, install Ubuntu from the Windows store, when you first run Ubuntu you will be asked to run a powershell script as admin: Once you have WSL setup, proceed with installing VSCode. #### Install VSCode VSCode can be installed at code.visualstudio.com. Once VSCode is open, you will want to configure VSCodes terminal to use `bash` instead of `powershell`. ![vscode settings](https://i.imgur.com/7cOsKuT.png) When VSCode is open, go to Settings. Click on the editor tab. ![switching to text settings](https://i.imgur.com/LfahcLm.png) and add the following line to your settings: ![pasting in the settings](https://i.imgur.com/nHqtllM.png) ``` "terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\bash.exe" ``` Toggle your VSCode terminal by pressing ```ctrl + ` ``` You should now see Ubuntu's default bash prompt instead of powershell. I recommened using `File > Open Folder` in VSCode before opening the terminal. This is because when you open the VSCode terminal, the bash prompt will start with the same directory you have open in VSCode as the current *working directory*. If you wish to debug and compile your code from the VSCode terminal see [Using the GNU Tools](https://hackmd.io/s/r13syBL74#Using-the-GNU-Tools). ## Using the GNU Tools If you followed the recommended configuration for your platform you will have all standardized to using the GNU Toolchain on your platform. All of the following instructions will be run from your terminal on MacOS/Linux or the VSCode terminal on Windows. #### Compiling You can compile code using the GNU C++ compiler by using the following syntax: ``` g++ ./prog.cpp -g -o prog ``` This will spit out your compiled code into your current working directory, you can run the code with: ``` ./prog ``` **Note:** * `./prog.cpp` refers to the path to your `.cpp file` * `-g` tells g++ to compile with gdb's debug flags * `-o` tells g++ that you wish to specify the output file. This will be your machine code. You *may* have to mark the code executable you can do this with: ``` chmod +x ./prog ``` ### Debugging using GDB If you compiled your code with the `-g` flag, you can debug your program using the GNU Debugger. ``` $: g++ ./prog.cpp -g -o prog $: gdb ./prog ``` When `gdb` starts your prompt will change from bash's `$:` to `gdb`'s `(gdb)`. ``` (gdb) ``` You will now have to execute your program from gdb. This works just like running your program from the terminal however you will replace the program name (arg0) with `run`. For example: If you are running your code with the good2 testcase and the `-v` flag: ``` $: ./prog -v good2 ``` You would do the same in `gdb`: ``` (gdb) run -v good2 ``` #### The Debugging Process The debugging process looks something like this: * Compile and Run program * Notice bug * Determine what part of a testcase is triggering the bug * Infer which line/lines in your program can be involved in your bug. * Recompile your program with `-g` and start gdb on your program * set *breakpoints* in gdb for the lines you believe to be causing the bug * Step through your program breakpoint by breakpoint and print out your variables to better understand what is happening in your code. #### Setting Breakpoints The main appeal to using a debugger is that you can step through your code line by line and see what the values of each variable is as your program executes. Knowing exactly what your variables are at a given point in execution grants important intuition into what is causing a bug in your program. When you first start gdb on your program and you see the gdb prompt you can set a breakpoint using `break`. ``` (gdb) break 142 Breakpoint 1 at 0x401185: file hello.cpp, line 142 ``` At some point, you may be compiling multiple `.cpp` files into eachother. ``` $: g++ prog.cpp lexer.cpp -g -o prog2 ``` You may want to set breakpoints in both files. You can do this in gdb using a similar syntax. ``` $: gdb prog2 ``` ``` (gdb) break prog.cpp:102 Breakpoint 1 at 0x408285: file prog.cpp, line 102 (gdb) break lexer.cpp:128 Note: breakpoint 1 also set at pc 0x401185. Breakpoint 2 at 0x408526: file lexer.cpp, line 128 ``` ### Navigating using GDB Once you set your breakpoints in gdb, run your program through gdb like you normally would, gdb will stop at your first breakpoint: ``` (gdb) run -v good3 Breakpoint 1, main (argc=1, argv=0x7fffffffd528) at prog.cpp:102 102 cout << x << endl; ``` Gdb stopped at our first breakpoint `prog.cpp:102`, here the variable `x` is used. We can see the value of `x` at this line of execution using the `print` command: ``` (gdb) print x $3 = "Hello there, C++ Programmers!" ``` Here we can see the third variable on our stack holds a `std::string` with the value `Hello there, C++ Programmers!`. This looks normal, so lets proceed to the next breakpoint using `next` or `n`. ``` (gdb) n (gdb) n Hello there, C++ Programmers! lexer.cpp:128 x* -> add(x, 1); ``` The first line is `stdout` this is what is actually printed to the terminal if you ran the code normally. ``` (gdb) print x $3 = "Hello there" (gdb) print x* 0x4011983742398 ``` The 2nd line is the line you set the your next breakpoint at. Here it looks like we're trying to add an integer to `std::string x`. In this hypothetical error, we are changing the value at the address of `x*` by using a the `add()` member function. Because this operates on a pointer the compiler may not see this as an error at compile time, and you would need gdb to catch this semantic error. ### Why use gdb? By stepping through and printing the variables in at select points in your program you can effectively jump around whereever you want and check what variable names are. There are instances where you will be passing data between many `.cpp` files that you compiled together. You can follow a data structure through each of the files by setting breakpoints wherever that data structure is modified. This process will help you quickly and effectively find where you are incorrectly modifying a data structure and how you can resolve the bug.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully