Re: Why is ld complaining?
Posted: Fri Nov 18, 2016 9:33 am
Hurrah! Proud of you.NunoLava1998 wrote:Meh, time to read a C book.
The Place to Start for Operating System Developers
http://f.osdev.org/
Hurrah! Proud of you.NunoLava1998 wrote:Meh, time to read a C book.
Lukand wrote:**Sigh**
Let me teach you a lesson (Pow!) about declaring, defining and usage of header files.
Header files are source code files which are used to make any function, variable, enum, #define, etc be public.
Declaring a function is thing that is required in header files, and required in some cases in normal source files.
Example:
Main.cppBasicarithmetic.cppCode: Select all
#include"myheader.h" #include<cstdio> int main() { printf("%d", add(2,3)); return 0; }
myheader.hCode: Select all
#include"myheader.h" int add(int a, int b) { return a+b; }
ORCode: Select all
#pragma once int add(int a, int b);
I hope we learned something today.Code: Select all
#pragma once int add(int, int);
Code: Select all
#include <stdio.h>
int main(){
int seventyone = 71;
int *pointertoseventyone = seventyone
printf(pointertoseventyone)
}
Code: Select all
#include <stdio.h>
int main(){
int seventyone = 71;
int *pointertoseventyone = seventyone
printf(*pointertoseventyone)
}
Code: Select all
kernel.o: In function `terminal_writestring':
kernel.c:(.text+0x160): undefined reference to `strlen'
Code: Select all
char strlen(const char[]);
Code: Select all
Installation process. Please add nasm, dd and ghost-i686-elf-tools to your "PATH" variable.
Current PATH:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;I:\ghost-i686-elf-tools\bin;C:\Users\(insert my username here)\AppData\Local\Microsoft\WindowsApps
Press any key to continue.
DOING THE BOOT.ASM COMPILATION
Any errors will be reported.
DOING THE KERNEL.C COMPILATION
Above with warnings ignored. Below with warnings not ignored. If there is nothing on both, your OS is practically perfect.
In file included from kernel.c:5:0:
include/kbd/kbd.c: In function 'getkey':
include/kbd/kbd.c:70:6: warning: variable 'key' set but not used [-Wunused-but-set-variable]
int key;
^
kernel.c: In function 'kernel_main':
kernel.c:10:2: warning: implicit declaration of function 'newlinef123' [-Wimplicit-function-declaration]
newlinef123();
^
kernel.c:14:2: warning: passing argument 1 of 'terminal_writestring' makes pointer from integer without a cast [enabled by default]
terminal_writestring(Key);
^
In file included from kernel.c:4:0:
include/tty/tty.c:54:6: note: expected 'const char *' but argument is of type 'int'
void terminal_writestring(const char* data) {
^
kernel.c: At top level:
kernel.c:16:6: warning: conflicting types for 'newlinef123' [enabled by default]
void newlinef123(void){
^
kernel.c:10:2: note: previous implicit declaration of 'newlinef123' was here
newlinef123();
^
kernel.c: In function 'newlinef123':
kernel.c:17:15: warning: operation on 'terminal_row' may be undefined [-Wsequence-point]
terminal_row = terminal_row++;
^
In file included from kernel.c:5:0:
include/kbd/kbd.c: In function 'getkey':
include/kbd/kbd.c:72:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
C++ mode, warnings not ignored.
In file included from kernel.c++:5:0:
include/kbd/kbd.c: In function 'short unsigned int getkey()':
include/kbd/kbd.c:70:6: warning: variable 'key' set but not used [-Wunused-but-set-variable]
int key;
^
include/kbd/kbd.c:72:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
kernel.c++: In function 'void kernel_main()':
kernel.c++:10:14: error: 'newlinef123' was not declared in this scope
newlinef123();
^
kernel.c++:14:26: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
terminal_writestring(Key);
^
In file included from kernel.c++:4:0:
include/tty/tty.c:54:6: error: initializing argument 1 of 'void terminal_writestring(const char*)' [-fpermissive]
void terminal_writestring(const char* data) {
^
kernel.c++: In function 'void newlinef123()':
kernel.c++:17:31: warning: operation on 'terminal_row' may be undefined [-Wsequence-point]
terminal_row = terminal_row++;
^
LINKING...
If anything fails, check your code.
kernel.o: In function `terminal_writestring':
kernel.c:(.text+0x160): undefined reference to `strlen'
collect2.exe: error: ld returned 1 exit status
If there were no errors, everything went fine.
That can't be right. You're missing semicolons and printf()'s first argument must a pointer to a format string. Further, even if you fix that, it would still not print "would print the memory location of "seventyone"" because seventyone is not &seventyone.NunoLava1998 wrote:The book, if you want to know (read until page 65. learned thatwould print the memory location of "seventyone",Code: Select all
#include <stdio.h> int main(){ int seventyone = 71; int *pointertoseventyone = seventyone printf(pointertoseventyone) }
Code: Select all
#include <stdio.h>
int main(void){
int seventyone = 71;
int *pointertoseventyone = &seventyone;
printf("%p\n", (void*)pointertoseventyone);
return 0;
}
Likewise, the correct code would be something like this:NunoLava1998 wrote: butwould print "71", for example.Code: Select all
#include <stdio.h> int main(){ int seventyone = 71; int *pointertoseventyone = seventyone printf(*pointertoseventyone) }
Code: Select all
#include <stdio.h>
int main(void){
int seventyone = 71;
int *pointertoseventyone = &seventyone;
printf("%d\n", *pointertoseventyone);
return 0;
}
I do remove %d\n, but it is there.alexfru wrote:That can't be right. You're missing semicolons and printf()'s first argument must a pointer to a format string. Further, even if you fix that, it would still not print "would print the memory location of "seventyone"" because seventyone is not &seventyone.NunoLava1998 wrote:The book, if you want to know (read until page 65. learned thatwould print the memory location of "seventyone",Code: Select all
#include <stdio.h> int main(){ int seventyone = 71; int *pointertoseventyone = seventyone printf(pointertoseventyone) }
The correct code after addressing all of the issues would be something like this:
Code: Select all
#include <stdio.h> int main(void){ int seventyone = 71; int *pointertoseventyone = &seventyone; printf("%p\n", (void*)pointertoseventyone); return 0; }
Likewise, the correct code would be something like this:NunoLava1998 wrote: butwould print "71", for example.Code: Select all
#include <stdio.h> int main(){ int seventyone = 71; int *pointertoseventyone = seventyone printf(*pointertoseventyone) }
If your book is teaching you to write wrong code, you may want a better book. Otherwise, you should pay attention to details because C is not a forgiving language.Code: Select all
#include <stdio.h> int main(void){ int seventyone = 71; int *pointertoseventyone = &seventyone; printf("%d\n", *pointertoseventyone); return 0; }
Hmmn, that is phrased a bit awkwardly, and ambiguously as well. Do you mean that the code was in the book, but you omitted it in the code you were compiling (whether intentionally or not), or that it was in both the book and the actual compiled code but was somehow dropped when you pasted or re-typed it into the message board's editor?NunoLava1998 wrote:I do remove %d\n, but it is there.
The book has "%d\n". However, i intentionally removed it. I made a example.Schol-R-LEA wrote:Hmmn, that is phrased a bit awkwardly, and ambiguously as well. Do you mean that the code was in the book, but you omitted it in the code you were compiling (whether intentionally or not), or that it was in both the book and the actual compiled code but was somehow dropped when you pasted or re-typed it into the message board's editor?NunoLava1998 wrote:I do remove %d\n, but it is there.
If the former, did you omit it deliberately, or by mistake, and did you try it with the code exactly as given as well?
If the latter, were you using cut and paste, or re-typing it, and if pasting it, did you do anything else that might have caused it to get dropped (again, whether intentionally or accidentally)?
My assumption is that it was accidental, regardless of where it took place; however, if it was intentional, I am curious as to the reason for it.
You made an error. I'm sure the book must have explained the purpose of the format string in printf().NunoLava1998 wrote:The book has "%d\n". However, i intentionally removed it. I made a example.
Code: Select all
*.o
*.O
*.obj
.sh
*.bak
*.*~
Code: Select all
#first, set the paths where the files are to be found
# binary toolchain
BIN := I:/ghost-i686-elf-tools/bin
#
# Dixium source code path - set it to the current directory the Makefile is in
SRCPATH := .
# system include headers
INCPATH := $(SRCPATH)/include:$(SRCPATH)/include/tty:$(SRCPATH)/include/kbd
# source code files required by the build
SRCFILES := $(SRCPATH)/kernel.c $(SRCPATH)/boot.asm $(INCPATH)/kbd/kbd.c $(INCPATH)/tty/tty.c
# header files required for the build
INCFILES := stdio.h stdlib.h include/tty/strings.h include/tty/vga.h include/kbd/stdio.h
# non-code files which should always be included with the code files
AUXFILES := Makefile README.md LICENSES
# list all the files that need to be distributed for the build, not
#including temporary files such as object or dependency files
DISTFILES := $(SRCFILES) $(INCFILES) $(AUXFILES)
# define the toolchain tools to be used and their settings
CC := $(BIN)/gcc
WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
-Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
-Wuninitialized -Wconversion -Wstrict-prototypes
# set the compile options used by all builds
CFLAGS := -g -O2 -std=c99 $(WARNINGS) -ffreestanding -I$(INCPATH)
# compile and/or link the sources into the files given by the target names
ASSEMBLE := nasm -t ELF
BUILD := $(CC) $(CFLAGS) -c $@
LINK := $(CC) $(CFLAGS) -o $@
#### build rules
#############
all: dixium
dixium: kernel kbd tty
$(LINK) $(OBJFILES)
kernel: boot.asm kernel.c
$(ASSEMBLE) boot.asm
$(BUILD) kernel.c
kbd: $(INCPATH)/kbd/kbd.c
$(BUILD) $(INCPATH)/kbd/kbd.c
tty: $(INCPATH)/tty/tty.c
$(BUILD) $(INCPATH)/kbd/tty.c
clean:
del *.o
Code: Select all
#define _GNU_SOURCE
#include "stdio.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>