I am new to ubuntu and hence to make file. I have successfully created make file with folder structure. However, if I add the similar structure, I get an error.
I successfully executed make command and ran application with one main cpp file and two files (list_menu.cpp and list_menu.h ) in sub folder cpp11_and_cpp14_menu
folder structure looks exactly like below where cpp11_and_cpp14.cpp has the main function.
../advancedcppproject/
cpp11_and_cpp14.cpp
Makefile
../advancedcppproject/cpp11_and_cpp14_menu
list_menu.cpp
list_menu.h
../advancedcppproject/multi_threading_example
multi_threading.cpp
multi_threading.h
Make file contents are
cpp11_and_cpp14 : cpp11_and_cpp14.o ./multi_threading_example/multi_threading.o ./cpp11_and_cpp14_menu/list_menu.o
g++ cpp11_and_cpp14.o ./multi_threading_example/multi_threading.o ./cpp11_and_cpp14_menu/list_menu.o -o cpp11_and_cpp14
cpp11_and_cpp14.o : cpp11_and_cpp14.cpp ./cpp11_and_cpp14_menu/list_menu.h ./multi_threading_example/multi_threading.h
g++ -c cpp11_and_cpp14.cpp
./cpp11_and_cpp14_menu/list_menu.0 : ./cpp11_and_cpp14_menu/list_menu.cpp ./cpp11_and_cpp14_menu/list_menu.h
g++ -c ./cpp11_and_cpp14_menu/list_menu.cpp
./multi_threading_example/multi_threading.o : ./multi_threading_example/multi_threading.cpp ./multi_threading_example/multi_threading.h
g++ -c ./multi_threading_example/multi_threading.cpp
After executing make command, it fails with an error message
g++ -c ./multi_threading_example/multi_threading.cpp
g++ cpp11_and_cpp14.o ./multi_threading_example/multi_threading.o ./cpp11_and_cpp14_menu/list_menu.o -o cpp11_and_cpp14
g++: error: ./multi_threading_example/multi_threading.o: No such file or directory
Makefile:6: recipe for target 'cpp11_and_cpp14' failed
make: *** [cpp11_and_cpp14] Error 1
I expect multi_threading.o to be created under folder ../advancedcppproject/multi_threading_example. However multi_threading.o is created under ../advancedcppproject.
Where as list_menu.o is correctly created under ../advancedcppproject/cpp11_and_cpp14_menu.
What is wrong?
Related
I'm running into a problem trying to write a Makefile for cross compilation for a Beaglebone. I'm using gcc-linaro 7.5.0 on a Windows 10 machine.
The problem occurs when I try to put all sources from the directory into a variable for later use.
SRCDIR = $(CURDIR)\source
SRCS := $(wildcard $(SRCDIR)\*.cpp
This and this have previously been posted and this solution has been accepted, however, I cannot seem to get it to work. This line #echo $(SRCS) gives me ECHO is off which makes me assume that I'm doing something wrong because $(SRCS) seems to be empty, hence the message(?) (source folder exists and it is not empty)
Then when the linker is called I get a message possibly also indicating that the directory seems to be empty which it is not.
"D:\UserData\User\DEV\gcc-linaro-7.5.0-2019.12-i686-mingw32_arm-linux-gnueabihf\bin\arm-linux-gnueabihf-g++.exe" -o hellobone -marm -O0 -g -I. -ID:\UserData\User\DEV\hellobone\include
arm-linux-gnueabihf-g++.exe: fatal error: no input files
compilation terminated.
make: *** [hellobone] Fehler 1
I've got a C++ program with a Makefile, building (g++) and running on Windows cmd. Thing is, sometimes it takes a while to run and save the results, and I want to run it with different parameters at the same time so that I can do something else while I wait for the first instance to finish. It doesn't work though, because of the executable I guess:
>make
g++ -c -o main.o main.cpp
Assembler messages:
Fatal error: can't create main.o: Permission denied
make: *** [main.o] Error 1
You have two problems: The one you ask about, and the reason you ask this question in the first place.
Lets start with the problem you have...
Judging by the Makefile you show, you have it all wrong.
Rules are in the format
target: sources_the_target_depend_on
The target is usually a file that need to be created. For an object file that is the name of the actual object file itself. The source files that the object files then depend on should be on the right-hand side.
To take an example from you Makefile (before you edited it away):
graph2: graph2.o
g++ -g -c graph.cpp -o graph2.o
Here you tell make that the file graph2 depends on the file graph2.o, and then it creates the graph2.o file. That's wrong. The rule should be that the file graph2.o depends om the file graph.cpp and go on to generate the file graph2.o:
graph2.o: graph.cpp
g++ -g -c graph.cpp -o graph2.o
This indirectly leads to the problem you have, with this line (deduced from your error and the Makefile):
main: main.o utils.o graph.o heuristics.o
g++ -g main.cpp -o main.o utils.o graph.o heuristics.o
This contains the same error as discussed above: You say that the file main depends on main.o and then the rule create main.o. Your rule should be
main: main.cpp utils.o graph.o heuristics.o
g++ -g main.cpp -o main utils.o graph.o heuristics.o
Note also how I no longer name the executable file main.o, as that is supposed to be used for object files.
Now lets continue with the reason you have the problem in the first place: That you need to edit the code to change data or values.
This is a problem that you need to solve. One common way to solve it is through command line arguments. If your program parses the command line arguments passed to your program you can pass it the values that could change from run to run.
How to do this is whole chapter on its own, so I wont give you any more details. There are plenty of tutorials online.
Lastly, you can simplify your Makefile considerably, by using implicit rules and variables.
I would simply create the Makefile to look something like this
# The compiler to use
CXX = g++
# Flags to pass to the compiler (add warnings when building)
CXXFLAGS = -Wall
# The main executable file to generate
TARGET = main
# List the object files needed to generate the main executable file
OBJECTS = main.o utils.o graph.o heuristics.o
# The all target depends on your main executable file
# Also as the first target in the Makefile, if no specific target is specified
# this will be the one that is used (it's the "default" target for the Makefile)
all: $(TARGET)
# The main executable file depends on the object files
$(TARGET): $(OBJECTS)
This is really it. the object files will be built automatically from their respective source files, and then the executable program will be linked using the object files listed.
I am trying to learn Fortran using the NetBeans IDE with the basic "Hello World" programming application. However, I keep getting "Build Failed". I don't know why? I have installed the MinGW. Then I configured the PATH in the Environment Variables. Finally, I installed NetBeans. This is what I programmed in NetBeans:
PRINT*, 'Hello World'
END
The following is the error and the history that I get:
cd 'C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1'
C:\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE=
SUBPROJECTS= .build-conf
make.exe[1]: Entering directory
`/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk
dist/Debug/MinGW-Windows/cppapplication_1.exe
make.exe[2]: Entering directory
`/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
mkdir -p build/Debug/MinGW-Windows
gfortran -c -g -o build/Debug/MinGW-Windows/testfortran.o
testfortran.f90
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_1
build/Debug/MinGW-Windows/main.o
build/Debug/MinGW-Windows/testfortran.o
build/Debug/MinGW-Windows/testfortran.o: In function `main':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2:
multiple definition of `main'
build/Debug/MinGW-Windows/main.o:C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/main.cpp:21:
first defined here
build/Debug/MinGW-Windows/testfortran.o: In function `MAIN__':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1:
undefined reference to `_gfortran_st_write'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1:
undefined reference to `_gfortran_transfer_character_write'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:1:
undefined reference to `_gfortran_st_write_done'
build/Debug/MinGW-Windows/testfortran.o: In function `main':
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2:
undefined reference to `_gfortran_set_args'
C:\Users\ABCD\Documents\NetBeansProjects\CppApplication_1/testfortran.f90:2:
undefined reference to `_gfortran_set_options'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/cppapplication_1.exe] Error
1
make.exe[2]: Leaving directory
`/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory
`/c/Users/ABCD/Documents/NetBeansProjects/CppApplication_1'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
I am not understanding how do I fix it? I would appreciate any help as I am a beginner to the Fortran/C++ world.
I followed the video tutorial you linked to in your comment, and I got the same build errors. Two unrelated changes are needed to resolve them:
To resolve the multiple definition of 'main' error, see the accepted answer to the Stack Overflow question gfortran multiple definition of main. Just copy and paste the source for the C++ and Fortran examples into your project files.
Rebuild your project and the multiple definition of 'main' error should be gone.
However, that does not fix the undefined reference errors. The solution for that is on the (old) NetBeans web site:
So, in the Linker window we when we add the reference to the Fortran
library by bringing up the Libraries window one will notice an Add
Options button on this page....here is where we put the -lgfortran
option under Other Options. Presto, the C code will compile and
doesn't complain about missing Fortran references.
The specific steps needed to fix the undefined reference errors are:
In the Projects window select your project, right click and select Properties from the context menu to open the Project Properties window.
Select Build > Linker from the Categories list.
On the right side of the Project Properties window click the ... button for Libraries.
In the Debug - Libraries window click the Add Option... button.
In the Select Option window:
Click the Other Option radio button.
Enter -lgfortran in the text field and press OK.
Click OK to close Debug - Libraries window. You should now see the
-lgfortran option displayed in the Project Properties window:
Having made the changes described above the projects builds without errors:
cd 'D:\NB82\CppApplication_5'
C:\msys\1.0\bin\make.exe -f Makefile CONF=Debug clean
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make.exe[1]: Entering directory `/d/NB82/CppApplication_5'
rm -f -r build/Debug
rm -f *.mod
make.exe[1]: Leaving directory `/d/NB82/CppApplication_5'
CLEAN SUCCESSFUL (total time: 558ms)
cd 'D:\NB82\CppApplication_5'
C:\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/NB82/CppApplication_5'
"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_5.exe
make.exe[2]: Entering directory `/d/NB82/CppApplication_5'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p build/Debug/MinGW-Windows
gfortran -c -g -Wall -o build/Debug/MinGW-Windows/newfortranFreeFormatFile.o newfortranFreeFormatFile.f90
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_5 build/Debug/MinGW-Windows/main.o build/Debug/MinGW-Windows/newfortranFreeFormatFile.o -lgfortran
make.exe[2]: Leaving directory `/d/NB82/CppApplication_5'
make.exe[1]: Leaving directory `/d/NB82/CppApplication_5'
BUILD SUCCESSFUL (total time: 1s)
Running the Fortran project then produces the expected output:
main in C++
FortMain
RUN SUCCESSFUL (total time: 2s)
You have created a C++ project then a fortran file.
Your IDE created a main in c++ for you. I guess it havent in the tutorial you have seen.
If i have seen it right then the answer is simple:
Just right click and delete this main.c and you are good :-)
OS: FreeBSD 11.0-RELEASE
I have the following directory structure:
/xxx/obj/
/xxx/src/deep.cpp
/xxx/flat.cpp
/xxx/makefile
The content of makefile is as follows:
flat.out: flat.o
deep.out: src/deep.o
I have no problem building flat:
/xxx $ make flat.out
c++ -O2 -pipe -c /xxx/flat.cpp -o flat.o
cc -O2 -pipe flat.o -o flat.out
/xxx $ ls obj
flat.o flat.out
But when I try to build deep it fails:
/xxx $ make deep.out
c++ -O2 -pipe -c /xxx/src/deep.cpp -o src/deep.o
error: unable to open output file 'src/deep.o': 'No such file or directory'
1 error generated.
*** Error code 1
Stop.
make: stopped in /xxx
If I then create /xxx/obj/src manually it succeeds:
/xxx $ mkdir obj/src
/xxx $ make deep.out
c++ -O2 -pipe -c /xxx/src/deep.cpp -o src/deep.o
cc -O2 -pipe src/deep.o -o deep.out
/xxx $ ls obj
deep.out flat.o flat.out src
/xxx $ ls obj/src
deep.o
According to this source bmake (aka bsdmake?) supports automatic creation of out-of-source OBJDIRs, but I cannot figure out how exactly.
How do I configure bmake to create the relevant directories automatically, in the general case?
Automatic creation of out-of-source OBJDIRs is supported but not in the sense you seem to expect it. Creation of out-of-source OBJDIRs can be implemented with bmake(1) directives but is not supported by the bmake(1) program itself. That said,
If you use bmake(1) directives shipped with FreeBSD to build your project, then the creation of OBJDIRs is supported and implemented by the obj target, so that the command make obj will create the OBJDIRs. See the file bsd.obj.mk for further documentation and implementation details. FreeBSD sources contain a lot of examples of programs using these directives to build.
If you use bmake(1) directives shipped as the (portable) bsdowl package, then the creation of OBJDIRs is supported and implemented by the target obj so that the command make obj or the command make preparatives using a more general target will create the OBJDIRs. The example directory contains several C-based examples showing how to prepare your Makefiles to build your project with the bsdowl package.
If you use custom bmake(1) directives, then you will have to implement a target taking care of this creation yourself. The file bsd.obj.mk is definitely a good source to get started with this.
An important point to note, is that bmake(1) determines its actual working directory before processing any targets, which means that it is almost always the right thing to do to execute the target requesting the creation of OBJDIRs on its own. Because of this, the command bmake obj all from your example would fail with the same error message you reported above. The correct invocation would instead be bmake obj && bmake all, since the second bmake(1) process has now the chance to change to the OBJDIR created by the previous run.
I have the following makefile when type make i got the following output. why is gcc gets called in this case?
nasm -felf ./source/multiboot.s
gcc multiboot.o -o multiboot
gcc: error: multiboot.o: No such file or directory
gcc: fatal error: no input files
compilation terminated.
make: *** [multiboot] Error 4
makefile:
CC=gcc
ASM=nasm
ASMFLAG=-felf
SOURCE=./source/
all: multiboot
multiboot.o: $(SOURCE)multiboot.s
$(ASM) $(ASMFLAG) $(SOURCE)multiboot.s
The "all" command depends on "multiboot", but there is no explicit rule defining how to produce "multiboot". In this case, Make uses a predefined rule that understands that, if the "$target.o" target exists, then "$target" can be constructed from "$target.o" by running the linker (in this case, GCC).
It seems like the problem in this case is that your instructions for the "multiboot.o" command does not actually produce the file "multiboot.o" as output. Try simply doing:
multiboot.o: multiboot.s
(That is, without specifying the command to run). Simply declaring this dependency should, by a similar mechanism, result in an implicit rule/command to create the "multiboot.o" output from "multiboot.s".