I tried to compile a simple C program with a header, a main, and another source file in Netbeans but it didn't work. I always get a huge error message.
I have absolutely no idea what I can do to make it work. Hope you guy can help me.
console:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/***/***/***/***/***/headertest'
nbproject/Makefile-Debug.mk:73: warning: overriding commands for target `build/Debug/GNU-Linux-x86/header.o'
nbproject/Makefile-Debug.mk:68: warning: ignoring old commands for target `build/Debug/GNU-Linux-x86/header.o'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/headertest
make[2]: Entering directory `//home/***/***/***/***/***/headertest'
nbproject/Makefile-Debug.mk:73: warning: overriding commands for target `build/Debug/GNU-Linux-x86/header.o'
nbproject/Makefile-Debug.mk:68: warning: ignoring old commands for target `build/Debug/GNU-Linux-x86/header.o'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/header.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/header.o.d" -o build/Debug/GNU-Linux-x86/header.o header.h
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/main.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.c
mkdir -p dist/Debug/GNU-Linux-x86
gcc -o dist/Debug/GNU-Linux-x86/headertest build/Debug/GNU-Linux-x86/header.o build/Debug/GNU-Linux-x86/header.o build/Debug/GNU-Linux-x86/main.o
/usr/bin/ld:build/Debug/GNU-Linux-x86/header.o: file format not recognized; treating as linker script
/usr/bin/ld:build/Debug/GNU-Linux-x86/header.o:1: syntax error
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/headertest] Error 1
make[2]: Leaving directory `/home/***/***/***/***/***/headertest'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/***/***/***/***/***/headertest'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 283ms)
Here is the rest of my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char** argv) {
lol();
return (EXIT_SUCCESS);
}
header.c
#include "header.h"
void lol(){
printf("lol");
}
header.h
#ifndef HEADER_H
#define HEADER_H
#ifdef __cplusplus
extern "C" {
#endif
void lol();
#ifdef __cplusplus
}
#endif
#endif /* HEADER_H */
You have not included stdio.h. Your lol() function doesn't know printf().
Put a #include <stdio.h> either into header.c or header.h.
Related
I have a simple kernel module with multiple objects. When I compile it, it prompts me that MODULE_LICENSE is missing. It's defined in the main.o but somehow undetected by the compiler:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
make[1]: Entering directory '/usr/src/kernels/5.14.0-105.el9.x86_64'
CC [M] /tmp/linux5-multi-files/util.o
LD [M] /tmp/linux5-multi-files/main.o
MODPOST /tmp/linux5-multi-files/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /tmp/linux5-multi-files/main.o
make[2]: *** [scripts/Makefile.modpost:150: /tmp/linux5-multi-files/Module.symvers] Error 1
make[2]: *** Deleting file '/tmp/linux5-multi-files/Module.symvers'
make[1]: *** [Makefile:1792: modules] Error 2
make[1]: Leaving directory '/usr/src/kernels/5.14.0-105.el9.x86_64'
make: *** [Makefile:5: build] Error 2
On older Linux (centos 6-7) it works fine, but on CentOS 8 and CentOS 9, it no longer works. What was wrong? I've attached all the files.
-- attached all files --
Makefile
main-objs := util.o
obj-m += main.o
build:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
clean:
make -C /lib/modules/`uname -r`/build M=`pwd` clean
main.c
#include "util.h"
static void __exit cleanup(void)
{
}
static int __init startup(void)
{
test();
return 0;
}
module_init(startup);
module_exit(cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux");
util.h
#ifndef UTIL_H
#define UTIL_H
#include <linux/module.h>
#include <linux/kernel.h>
void test(void);
#endif
util.c
#include "util.h"
void test()
{
printk(KERN_INFO "whatever\n");
}
To further debug this issue, I've added -v to the ccflags, and main.c is not even compiled on CentOS 8!
So far it looks like the module name can't be the same as the object name, so I have to modify the makefile to something like this:
xxx-objs := main.o util.o
obj-m += xxx.o
And the problem is gone.
I am trying to compile my C program using make and I've come across this problem that I can't quite understand. I have 3 files in the 'calc' folder of my project: add.c sub.c and main.c. I have my Makefile located in the root folder of my project, which has the calc folder that I mentioned in it. This is what my Makefile looks like:
CC=gcc
OBJECTS=obj/main.o obj/add.o obj/sub.o
elf/new: ${OBJECTS}
${CC} -o elf/new ${OBJECTS}
obj/main.o: calc/main.c
${CC} -c -g calc/main.c -o obj/main.o
obj/add.o: calc/add.c
${CC} -c -g calc/add.c -o obj/add.o
obj/sub.o: calc/sub.c
${CC} -c -g calc/sub.c -o obj/sub.o
clean:
rm obj/${OBJECTS} elf/new
When I type 'make' into the terminal to compile, I get an error like this:
gcc -c -g calc/add.c -o obj/add.o
gcc -c -g calc/sub.c -o obj/sub.o
gcc -o elf/new obj/main.o obj/add.o obj/sub.o
obj/add.o: In function `add':
/home/bigger/workspace/test/calc/add.c:1: multiple definition of `add'
obj/main.o:/home/bigger/workspace/test/calc/add.c:1: first defined here
obj/sub.o: In function `sub':
/home/bigger/workspace/test/calc/sub.c:1: multiple definition of `sub'
obj/main.o:/home/bigger/workspace/test/calc/sub.c:1: first defined here
collect2: error: ld returned 1 exit status
makefile:5: recipe for target 'elf/new' failed
make: *** [elf/new] Error 1
And my code are there:
bigger#linux:~/workspace/test> cat calc/add.c
int add(int a, int b){
return a+b;
}
bigger#linux:~/workspace/test> cat calc/sub.c
int sub(int a, int b) {
return a-b;
}
bigger#linux:~/workspace/test> cat calc/main.c
#include <stdio.h>
#include "add.c"
#include "sub.c"
int main(int argc, char* argv[])
{
int a = 10;
int b = 5;
printf("add: %d\nsub:%d\n", a+b, a-b);
return 0;
}
When you include it is making the functions add and sub part of your main.c, then when you make you are linking main (which already has the functions by include) to the add and sub objects which have the same function symbols. You need to include header files with function declarations rather than include function definitions. See http://www.cprogramming.com/declare_vs_define.html for a longer discussion.
If I manually compile the code below, I got no error:
/*
* File: newmain.c
* Author: Mike
*
* Created on September 18, 2015, 7:36 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
printf ("Hello!");
return 0;
}
However, by doing the with NetBeans, I got the following error:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe
make[2]: Entering directory '/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f "build/Debug/Cygwin_4.x-Windows/newmain.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/newmain.o.d" -o build/Debug/Cygwin_4.x-Windows/newmain.o newmain.c
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++ -o dist/Debug/Cygwin_4.x-Windows/cppapplication_1 build/Debug/Cygwin_4.x-Windows/main.o build/Debug/Cygwin_4.x-Windows/newmain.o
build/Debug/Cygwin_4.x-Windows/newmain.o: In function `main':
/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1/newmain.c:14: multiple definition of `main'
build/Debug/Cygwin_4.x-Windows/main.o:/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1/main.cpp:15: first defined here
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:63: recipe for target 'dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1'
nbproject/Makefile-Debug.mk:60: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/Mike/Documents/NetBeansProjects/CppApplication_1'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
How can I get this code being executed in the NetBeans too?
I am pretty new to programming and I am trying to learn C. I installed net beans 8.0.2 on a mac with OS 10.10.3. And I tried to run my first code:
/* HelloWorld.c */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello PROGC"); /* prints > Hello PROGC */
return EXIT_SUCCESS;
}
Now I am getting this Error Message:
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/cppapplication_1
mkdir -p build/Debug/GNU-MacOSX
rm -f "build/Debug/GNU-MacOSX/Hello World.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/Hello World.o.d" -o build/Debug/GNU-MacOSX/Hello\ World.o Hello\ World.c
mkdir -p dist/Debug/GNU-MacOSX
gcc -o dist/Debug/GNU-MacOSX/cppapplication_1 build/Debug/GNU-MacOSX/Hello\ World.o build/Debug/GNU-MacOSX/main.o
duplicate symbol _main in:
build/Debug/GNU-MacOSX/Hello World.o
build/Debug/GNU-MacOSX/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [dist/Debug/GNU-MacOSX/cppapplication_1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 687ms)
Could you please let me know what I have to change? Many thanks in advance!
I want to separate my kernel module to sources. But I can't compile it properly.
print_hello.h
#ifndef PRINT_HELLO_H_
#define PRINT_HELLO_H_
void print_hello(void);
#endif /* PRINT_HELLO_H_ */
print_hello.c:
#include <linux/kernel.h>
#include "print_hello.h"
void print_hello(void) {
printk("Hello, World!\n");
}
main.c:
#include<linux/module.h>
#include<linux/version.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include "print_hello.h"
int hello_init(void) {
print_hello();
return 0;
}
module_init(hello_init);;
MODULE_LICENSE("GPL");
Makefile:
obj-m += main.o
main-objs := print_hello.o main.o
all:
make -C /lib/modules/3.2.51/build M=$(PWD) modules
clean:
make -C /lib/modules/3.2.51/build M=$(PWD) clean
but when I compile this program this error occur:
root#linux:r# make
make -C /lib/modules/3.2.51/build M=/home/root/hello modules
make[1]: Entering directory `/usr/src/build'
CC [M] /home/root/hello/main.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "print_hello" [/home/root/hello/main.ko] undefined!
LD [M] /home/root/hello/foo.ko
make[1]: Leaving directory `/usr/src/build'
Any solution to solve this problem?