-
April 6th, 2015, 12:57 PM
#1
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 ?
Last edited by flex567; April 7th, 2015 at 10:28 AM.
-
April 6th, 2015, 02:23 PM
#2
Re: compile with terminal from MyProject folder
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)
-
April 6th, 2015, 02:45 PM
#3
Re: compile with terminal from MyProject folder
-
April 7th, 2015, 12:06 AM
#4
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.
-
April 7th, 2015, 08:55 AM
#5
Re: compile with terminal from MyProject folder
Originally Posted by laserlight
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.
-
April 7th, 2015, 09:51 AM
#6
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
-
April 7th, 2015, 10:37 AM
#7
Re: compile with terminal from MyProject folder
-Iheader = -IMyProject/header
or just
-Iheader = -Iheader
I tried both and none worked.
-
April 7th, 2015, 10:49 AM
#8
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.
-
April 7th, 2015, 11:21 AM
#9
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.
-
April 7th, 2015, 11:29 AM
#10
Re: compile with terminal from MyProject folder
from the MyProject folder, can you do the following command and copy/paste the output ?
ls -alR
-
April 7th, 2015, 12:24 PM
#11
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
-
April 7th, 2015, 02:35 PM
#12
Re: compile with terminal from MyProject folder
You missed typed the name in the header folder:
Funcitions.h
-
April 8th, 2015, 02:28 AM
#13
Re: compile with terminal from MyProject folder
oh, I see. I thought something was wrong with the command.
-
April 8th, 2015, 10:20 AM
#14
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.
-
April 8th, 2015, 10:56 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|