I make a premise: I'm a "newborn" in the Linux world, I have very little experience. I decided to switch to this operating system after studying it in a university course and I fell in love with it.
Having said that, let me tell you my problem...
I installed Linux Mint 21.1 initially in dual-boot with a fairly small partition size. To extend the partition I thought of reinstalling it from 0 thus avoiding some problems that were appearing. For university reasons I find myself programming and compiling from a terminal. At the first installation of Linux I managed to install everything necessary and to solve the various problems that arose. On reinstallation, when I try to compile a file from the terminal, I get the following message:
gcc -c procedure.c
gcc -c semafori.c
gcc -c prodcons_singolo_buffer.c
gcc -o prodcons_singolo_buffer procedure.o semafori.o prodcons_singolo_buffer.o
/usr/bin/ld: impossibile trovare -lc: File o directory non esistente
collect2: error: ld returned 1 exit status
make: *** [Makefile:4: prodcons_singolo_buffer] Errore
In particular, the error Is:
usr/bin/ld: impossibile trovare -lc: File o directory non esistente
Unfortunately I can't find anything about it on the web. Thank you all for your availability and for any response.
EDIT:
This is Makefile:
all: prodcons_singolo_buffer
prodcons_singolo_buffer: procedure.o semafori.o prodcons_singolo_buffer.o
gcc -o prodcons_singolo_buffer procedure.o semafori.o prodcons_singolo_buffer.o
prodcons_singolo_buffer.o: prodcons_singolo_buffer.c
gcc -c prodcons_singolo_buffer.c
procedure.o: procedure.h procedure.c
gcc -c procedure.c
semafori.o: semafori.c semafori.h
gcc -c semafori.c
clean:
rm -rf *.o
rm -rf prodcons_singolo_buffer
I agree that your compiler/library install is borked.
Unrelated but here is how I would write your Makefile (untested). Let me know when you tried it out and I will delete this answer.
.PHONY: all clean
all: prodcons_singolo_buffer
prodcons_singolo_buffer: procedure.o semafori.o prodcons_singolo_buffer.o
procedure.o: procedure.c procedure.h
semafori.o: semafori.c semafori.h
clean:
rm -f prodcons_singolo_buffer *.o
Related
I am trying to use a make file that my professor provided, but that make file is made to work in an Arch Linux VM, and not my Mac environment. To solve this, I've downloaded gmake and gcc via homebrew, and swapped them out as the make and compiler targets in my CLion toolchain. However, when I try to build, I am still getting the error "ld: unknown option: -rpath=$ORIGIN". This is the line that is failing (line 10) everytime:
$(CC) $(CFLAGS) $(LDFLAGS) -DDEBUG=$(DEBUG) -L. -Wl,-rpath='$$ORIGIN' -driverfile myfile.c -o $#
Here is my output when I hit build:
====================[ Build | all ]=============================================
/usr/local/Cellar/make/4.3/bin/gmake --jobs=9 all
/usr/local/Cellar/gcc/11.2.0_3/bin/gcc-11 -Wall -g -DDEBUG=1 -L. -Wl, -rpath='$ORIGIN' -driverfile myfile.c -o myfile
ld: unknown option: -rpath=$ORIGIN
collect2: error: ld returned 1 exit status
gmake: *** [Makefile:10: myfile] Error 1
I am allowed to modify the makefile for testing purposes on my local machine, so long as all the stuff still works with the original makefile on the Arch Linux VM, and I'm happy to do this if it's the only solution, but I'd much rather figure out why its not working as is. (Even if the answer is 'Bc its a Mac and Apple says so.')
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm really desperate when trying to write a C makefile for windows for my school project. I already have my makefile written and used for ubuntu and it works perfectly like this:
all: clean install
install: dynarr.o broadcaster.o main.o
gcc -o freq dynarr.o broadcaster.o main.o
clean:
rm -f *.o
dynarr.o:
gcc -c dynarr.c
broadcaster.o:
gcc -c broadcaster.c
main.o:
gcc -c main.c
I've already tried just about 10 tutorials on various sites and it is getting on my nerve.
I need to compile dynarr.c broadcaster.c main.c in this particular order and the output should be freq.exe. Compiler on the school server is gcc for both versions(linux and windows).
Please help me.
Edit: For some reason our teacher's online validator required freq.exe for both windows and linux and he did not bother to mention it anywhere. Everything I had to edit was freq to freq.exe. Thanks for support.
On Windows, you expect the compiler to produce a file named freq.exe (or the test system does, anyway) but the command you issue tells GCC to emit a file named just freq, instead. The latter seems natural for Linux, so it is not surprising that your build works as expected there.
It is tricky to support different OS families with the same makefile, which is why utilities such as the Autotools and CMake were invented. Supposing that you only need a Makefile for Windows, this variation should suffice:
all: clean install
install: dynarr.o broadcaster.o main.o
gcc -o freq.exe dynarr.o broadcaster.o main.o
clean:
rm -f *.o
dynarr.o:
gcc -c dynarr.c
broadcaster.o:
gcc -c broadcaster.c
main.o:
gcc -c main.c
With that said, I have some suggestions for further improvement:
A rule for building a specific output file should name that file as its target.
Conversely, targets of rules that do not correspond to a file created by that rule should be declared "phony" by naming them as prerequisites of a target named .PHONY.
By convention, an "install" target copies built files from their build location to a permanent system location. Neither your install rule nor anything else in your makefile does that, so you probably don't need an "install" target.
It is perhaps intentional that your rule for the default target cleans before it builds, but that is abnormal, as it defeats one of the major reasons for using make in the first place: to avoid needless work.
It is good form and it protects against errors to use Make's automatic variables to avoid repeating yourself in rules.
There is some divergence of opinion, but I think the general consensus is that a clean target should clean up all built objects, not just intermediate ones.
Applying all those would yield a makefile such as this:
all: freq.exe
freq.exe: dynarr.o broadcaster.o main.o
gcc -o $# $^
clean:
rm -f freq.exe *.o
dynarr.o:
gcc -c dynarr.c
broadcaster.o:
gcc -c broadcaster.c
main.o:
gcc -c main.c
.PHONY: all clean
Personally, I would also suggest introducing variables for the executable target (which would make it easier to adapt the makefile between Windows and Linux / macOS) and for the required object files (which is typical, and which would allow the clean target to be more precise). You could also consider relying on Make's built-in rules for building .o files from .c files, as the explicit rules you're using don't do anything that the is not also done by the built-in one. The result might be:
PROG = freq.exe
OBJS = dynarr.o broadcaster.o main.o
all: $(PROG)
$(PROG): $(OBJS)
gcc -o $# $^
clean:
rm -f $(PROG) $(OBJS)
# Relies on the built-in rules for building object files from C sources
.PHONY: all clean
I'm on a Windows machine and use MinGW, attempting to compile a hello world program that uses a shared library. After an absurd amount of attempts, I found out the following:
Manually compiling it with gcc and providing -I and -L flags for the required directories works fine.
Using the msys make.exe file provided under the MinGW/msys/1.0/bin installation directory properly executes a Makefile with no problems
Using the mingw32-make.exe provided under MinGW/bin doesn't work properly when trying to build using the same Makefile. From my understanding, it doesn't parse the -I and -L flags at all. It works fine if I add the dependencies (both includes and libs) under their respective MinGW directories.
These past few days while I was trying and familiarizing myself with these tools (I'm comfortable with C's syntax but know about nothing past that) I read many guides and no one seemed to have this issue (from the few that actually attempted this on a Windows machine without using an IDE). Did I miss something? Is my MinGW installation known to have this issue?
Note that at first I was attempting to compile the project using the 64-bit version of the library but failed. I'm guessing this means that I have a 32-bit MinGW installation.
Knowing that some will ask to see the Makefile:
CC = gcc
MY_LIB = -L/e/C_Projects/Libraries/MySharedLib/lib -lMyLibName
MY_INCLUDE = -I/e/C_Projects/Libraries/MySharedLib/include
CFLAGS = -Wall -c $(MY_INCLUDE)
LDFLAGS = -lmingw32 -mwindows $(MY_LIB)
EXE = Test.exe
all: $(EXE)
$(EXE): main.o
$(CC) $< $(LDFLAGS) -o $#
main.o: main.c
$(CC) $(CFLAGS) $< -o $#
clean:
del *.o && del $(EXE)
The error produced by mingw32-make.exe is the following
main.c:1:22: fatal error: MyLib.h: No such file or directory
#include <MyLib.h>
^
compilation terminated.
Makefile:19: recipe for target 'main.o' failed
mingw32-make: *** [main.o] Error 1
My teacher is not the best at explain C so I'm having a bit of trouble understanding the connection of makefiles. I have already added the code for complex.c, complex.h, and main.c. I'm just having trouble compiling it all using the make command. I followed the example on the powerpoint he handed up and I don't understand why its failing to get to complex.
makefile
complex: main.o complex.o
gcc -o complex main.o complex.o
main.o: main.c complex.h
gcc -c main.c -lm
complex.o: complex.c complex.h
gcc -c complex.c -lm
clean:
rm*.o complex
ls
main.o
main.o: complex.h
gcc -c main.c
complex.o
complex.o: complex.h
gcc -c complex.c
Error
mason% make
gcc -o complex main.o complex.o
ld: fatal: file main.o: unknown file type
ld: fatal: file processing errors. No output written to complex
collect2: error: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `complex'
It looks like you have put Makefile fragments inside main.o and complex.o. These should be generated by the compiler, not by you.
Delete these files, and make again.
Additionally, your make clean rule is missing a space.
clean:
rm *.o complex
ls
One more thing. No need for -lm in the compile lines.
main.o: main.c complex.h
gcc -c main.c
complex.o: complex.c complex.h
gcc -c complex.c
You should add -lm at the linking phase.
complex: main.o complex.o
gcc -o complex main.o complex.o -lm
The "Makefile" defines and controls the build dependencies.
For example, you can't build the main executable binary without first building the binary object/module files that go with it. In this case, those are main.o and complex.o.
Generally any object file you need also needs a rule (though some rules can use "wildcards" to build more).
This is all rather academic. Best to take errors at their word and try to disprove them (this one basically says that main.o exists and is incorrect). In this case the hypothesis that main.o exists is supported by the fact that it didn't compile when you ran the make command.
Until you learn more you could invoke "make" using "targets". Like: make clean and make complex. It might help bring clarity.
A lot of makefiles put an "all" target to sort of reset the build. That then depends on "clean" and the executable and library targets. Like:
all: clean complex
So then you "make all" to clean and build.
A good tutorial is here. Mrbook Makefile Tutorial
Alright, this is for a school project where I am supposed to complete a skeleton program provided by the professor. Here is the makefile as provided to me:
db: db.o students.o courses.o enrolls.o
cc db.o students.o courses.o enrolls.o -o db
db.o: db.c types.h students.h courses.h enrolls.h db.h
cc -c db.c
students.o: students.c types.h students.h
cc -c students.c
courses.o: courses.c types.h courses.h
cc -c courses.c
enrolls.o: enrolls.c types.h students.h courses.h enrolls.h
cc -c enrolls.c
Now, this worked fine in the commandline (with gcc) and in Dev C++, but when I tried to use Netbeans it threw an error, saying something about the clean command. So, I added this line:
clean:
rm -f *.exe *.o
to the end of the file, and it worked fine (in Netbeans). But, it still won't work in Eclipse CDT. It gives this error:
Description Resource Path Location Type
make: *** No rule to make target `all'. Stop. Course Project C/C++ Problem
So, I tried adding
all:db
to the top, but then it throws this error:
Description Resource Path Location Type
make: *** [db] Error 1 Course Project C/C++ Problem
So, now I'm at a loss for what to do. I've Googled around, but nothing has seemed to work yet. Any ideas on how to change this makefile so it works in Eclipse?
Here is the makefile in its current (non-functional) form:
all:db
db: db.o students.o courses.o enrolls.o
cc db.o students.o courses.o enrolls.o -o db
db.o: db.c types.h students.h courses.h enrolls.h db.h
cc -c db.c
students.o: students.c types.h students.h
cc -c students.c
courses.o: courses.c types.h courses.h
cc -c courses.c
enrolls.o: enrolls.c types.h students.h courses.h enrolls.h
cc -c enrolls.c
clean:
rm -f *.exe *.o
As far as I know, Eclipse use gnu make/gcc as a default build toolchain. So if your makefile works in a shell it should work in Eclipse. The first error you mention just points out that Eclipse build with the default command "make all". Adding 'all: db' should have corrected this problem. As #Bug Catcher said you should have a space between 'all:' and 'db'. You can also add a .PHONY statement :
.PHONY: all db clean
all: db
#echo "Done !"
# [...]