CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    compile with terminal from MyProject folder

    my folder structure looks like this:
    Code:
             
                        MyProject 
                |                           |
             header                        source
                |                           |
           Dog.h,Functions.h     Functions.cpp, Dog.cpp, main.cpp

    How can I compile and run this program (what do I have to type in terminal) - I would like to compile and run it from MyPoject's directory
    Code:
    cd MyProject
    ?
    Last edited by flex567; April 7th, 2015 at 10:28 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: compile with terminal from MyProject folder

    and your compiler is ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    gcc

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: compile with terminal from MyProject folder

    I would expect something like this:
    Code:
    g++ -Wall -o myproject source_files/functions_cpp.cpp source_files/main.cpp
    Of course, you might specify more than just -Wall, e.g., -pedantic -std=c++11. You might also specify some higher optimisation level, e.g., -O2. For more details, read the GCC online documentation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    Quote Originally Posted by laserlight View Post
    I would expect something like this:
    Code:
    g++ -Wall -o myproject source_files/functions_cpp.cpp source_files/main.cpp
    Of course, you might specify more than just -Wall, e.g., -pedantic -std=c++11. You might also specify some higher optimisation level, e.g., -O2. For more details, read the GCC online documentation.
    This doesn't work.

    Afer this command that you suggested
    Code:
    g++ -o myproject source/functions.cpp source/main.cpp
    I get this error:
    Code:
    source/functions.cpp:1:17: fatal error: test.h: No such file or directory
     #include"test.h"
                     ^
    compilation terminated.
    source/main.cpp:2:18: fatal error: test.h: No such file or directory
     #include "test.h"
                      ^
    compilation terminated.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: compile with terminal from MyProject folder

    You need to specify where the headers are located (looking at the help for gcc would show this):

    Code:
    g++ -Wall -o myproject -Iheader source_files/functions_cpp.cpp source_files/main.cpp

  7. #7
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    -Iheader = -IMyProject/header

    or just

    -Iheader = -Iheader


    I tried both and none worked.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: compile with terminal from MyProject folder

    -Iheader

    Are you getting the same errors ? What operating system are you using ? On Linux systems, filenames are case sensitive.

  9. #9
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    the command and the error:

    Code:
    g++ -Wall -o myproject -Iheader source/Functions.cpp source/Dog.cpp source/main.cppsource/Functions.cpp:1:23: fatal error: Functions.h: No such file or directory
     #include "Functions.h"
                           ^
    compilation terminated.
    source/Dog.cpp:3:15: error: expected initializer before ‘Bark’
     void Dog::Dog Bark(){
                   ^
    source/main.cpp:3:23: fatal error: Functions.h: No such file or directory
     #include "Functions.h"
                           ^
    compilation terminated.

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: compile with terminal from MyProject folder

    from the MyProject folder, can you do the following command and copy/paste the output ?

    ls -alR

  11. #11
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    Code:
    ls -alR 
    .:
    total 16
    drwxrwxr-x 4 prog prog 4096 Apr  7 16:24 .
    drwxr-xr-x 3 prog prog 4096 Apr  6 17:06 ..
    drwxrwxr-x 2 prog prog 4096 Apr  7 17:17 header
    drwxrwxr-x 2 prog prog 4096 Apr  7 17:17 source
    
    ./header:
    total 16
    drwxrwxr-x 2 prog prog 4096 Apr  7 17:17 .
    drwxrwxr-x 4 prog prog 4096 Apr  7 16:24 ..
    -rw-rw-r-- 1 prog prog   91 Apr  7 17:03 Dog.h
    -rw-rw-r-- 1 prog prog   94 Apr  7 17:15 Funcitions.h
    
    ./source:
    total 20
    drwxrwxr-x 2 prog prog 4096 Apr  7 17:17 .
    drwxrwxr-x 4 prog prog 4096 Apr  7 16:24 ..
    -rw-rw-r-- 1 prog prog   76 Apr  7 17:13 Dog.cpp
    -rw-rw-r-- 1 prog prog  167 Apr  7 17:17 Functions.cpp
    -rw-rw-r-- 1 prog prog  117 Apr  7 17:16 main.cpp

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: compile with terminal from MyProject folder

    You missed typed the name in the header folder:

    Funcitions.h

  13. #13
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    oh, I see. I thought something was wrong with the command.

  14. #14
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: compile with terminal from MyProject folder

    If I would have another folder 'resources' and inside this folder would be text.txt and in my code would be ifstream IN(path).
    What should I put in my string variable path to link the IN object to the text file? My source code is in the source folder and text.txt is in resources folder and everything is in the MyProject folder.

  15. #15
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: compile with terminal from MyProject folder

    It depends on where you execute the executable from (and maybe even if you execute from an IDE or not).

    If you execute from the terminal in the MyProject folder it would probably be:

    Code:
    ifstream in("resources/text.txt");
    if you execute from the folder above MyProject, it would probably be:
    Code:
    ifstream in("MyProject/resources/text.txt");

    One method would be to obtain the executable name from argv[0] and
    parse it to get the folder that the executable resides in.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured