C Programming with NetBeans on Mac OSX - c

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!

Related

MODULE_LICENSE is defined but undetected in linux kernel 5.X

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.

Ardupilot Sik(firmware for the Si1000) installation error

On running a make install in the Firmware folder of https://github.com/ArduPilot/SiK/tree/8a690a28647ee6085e7dce456ab3963a350acb2d on an OSX, I get:
% build bootloader for hm_trp
obj/hm_trp/bootloader~hm_trp/bootloader.dep:1: *** missing separator. Stop.
make: *** [build~bootloader~hm_trp] Error 2
Looks like it is some Makefile formatting issue but I could not lay may hands on it.
On deleting obj directory created under firmware and running make install again I run into:
Error on OSX
% build bootloader for hm_trp
CC bootloader/bootloader.c
bootloader/bootloader.c:102: error 158: overflow in implicit constant conversion
make[1]: *** [obj/hm_trp/bootloader~hm_trp/bootloader.rel] Error 1
make: *** [build~bootloader~hm_trp] Error 2
Detailed Error on OSX
% build bootloader for hm_trp
AS bootloader/crtstart.asm
cp bootloader/crtstart.asm .//obj/hm_trp/bootloader~hm_trp/crtstart.asm
sdas8051 -jloscp .//obj/hm_trp/bootloader~hm_trp/crtstart.asm
(/bin/echo -n .//obj/hm_trp/bootloader~hm_trp/ && sdcc -mmcs51 -MM -DBL_VERSION=2 --model-small --no-xinit-opt --opt-code-size --Werror -DBOARD_hm_trp -I.//include -V bootloader/bootloader.c) > obj/hm_trp/bootloader~hm_trp/bootloader.dep
CC bootloader/bootloader.c
sdcc -mmcs51 -c -o obj/hm_trp/bootloader~hm_trp/bootloader.rel -DBL_VERSION=2 --model-small --no-xinit-opt --opt-code-size --Werror -DBOARD_hm_trp -I.//include -V bootloader/bootloader.c
+ /usr/local/bin/sdcpp -nostdinc -Wall -std=c11 -DBL_VERSION=2 -Werror -DBOARD_hm_trp -I.//include -obj-ext=.rel -D__SDCC_CHAR_UNSIGNED -D__SDCC_MODEL_SMALL -D__SDCC_FLOAT_REENT -D__SDCC=3_8_0 -D__SDCC_VERSION_MAJOR=3 -D__SDCC_VERSION_MINOR=8 -D__SDCC_VERSION_PATCH=0 -DSDCC=380 -D__SDCC_REVISION=10562 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__=1 -D__STDC_NO_THREADS__=1 -D__STDC_NO_ATOMICS__=1 -D__STDC_NO_VLA__=1 -D__STDC_ISO_10646__=201409L -D__STDC_UTF_16__=1 -D__STDC_UTF_32__=1 -isystem /usr/local/bin/../share/sdcc/include/mcs51 -isystem /usr/local/share/sdcc/include/mcs51 -isystem /usr/local/bin/../share/sdcc/include -isystem /usr/local/share/sdcc/include bootloader/bootloader.c
bootloader/bootloader.c:102: error 158: overflow in implicit constant conversion
make[1]: *** [obj/hm_trp/bootloader~hm_trp/bootloader.rel] Error 1
make: *** [build~bootloader~hm_trp] Error 2
I have tried the installation on linux too and it raises:
Error on Linux
% build bootloader for rfd900a
make[1]: Entering directory 'SiK/Firmware'
AS bootloader/crtstart.asm
CC bootloader/bootloader.c
bootloader/bootloader.c:102: error 158: overflow in implicit constant conversion
make[1]: *** [include/rules.mk:135: obj/rfd900a/bootloader~rfd900a/bootloader.rel] Error 1
make[1]: Leaving directory 'SiK/Firmware'
make: *** [Makefile:95: build~bootloader~rfd900a] Error 2
Detailed Error on Linux
% build bootloader for rfd900a
make[1]: Entering directory 'SiK/Firmware'
AS bootloader/crtstart.asm
cp bootloader/crtstart.asm .//obj/rfd900a/bootloader~rfd900a/crtstart.asm
sdas8051 -jloscp .//obj/rfd900a/bootloader~rfd900a/crtstart.asm
(/bin/echo -n .//obj/rfd900a/bootloader~rfd900a/ && sdcc -mmcs51 -MM -DBL_VERSION=2 --model-small --no-xinit-opt --opt-code-size --Werror -DBOARD_rfd900a -I.//include -V bootloader/bootloader.c) > obj/rfd900a/bootloader~rfd900a/bootloader.dep
CC bootloader/bootloader.c
sdcc -mmcs51 -c -o obj/rfd900a/bootloader~rfd900a/bootloader.rel -DBL_VERSION=2 --model-small --no-xinit-opt --opt-code-size --Werror -DBOARD_rfd900a -I.//include -V bootloader/bootloader.c
+ /usr/bin/sdcpp -nostdinc -Wall -std=c11 -DBL_VERSION=2 -Werror -DBOARD_rfd900a -I.//include -obj-ext=.rel -D__SDCC_CHAR_UNSIGNED -D__SDCC_MODEL_SMALL -D__SDCC_FLOAT_REENT -D__SDCC=3_8_0 -D__SDCC_VERSION_MAJOR=3 -D__SDCC_VERSION_MINOR=8 -D__SDCC_VERSION_PATCH=0 -DSDCC=380 -D__SDCC_REVISION=10562 -D__SDCC_mcs51 -D__STDC_NO_COMPLEX__=1 -D__STDC_NO_THREADS__=1 -D__STDC_NO_ATOMICS__=1 -D__STDC_NO_VLA__=1 -D__STDC_ISO_10646__=201409L -D__STDC_UTF_16__=1 -D__STDC_UTF_32__=1 -isystem /usr/bin/../share/sdcc/include/mcs51 -isystem /usr/share/sdcc/include/mcs51 -isystem /usr/bin/../share/sdcc/include -isystem /usr/share/sdcc/include bootloader/bootloader.c
bootloader/bootloader.c:102: error 158: overflow in implicit constant conversion
make[1]: *** [include/rules.mk:135: obj/rfd900a/bootloader~rfd900a/bootloader.rel] Error 1
make[1]: Leaving directory 'SiK/Firmware'
make: *** [Makefile:95: build~bootloader~rfd900a] Error 2
https://github.com/ArduPilot/SiK/blob/master/Firmware/bootloader/bootloader.c#L102
Downgrading SDCC solves the issues. Check https://github.com/ArduPilot/SiK/issues/50#issuecomment-467874478.

Netbeans Error when running a simple c program

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?

using header in netbeans C

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.

Cross-Compiling with mingw for Win on Linux a pthread

I try do compile a simple thread program for Win on Linux with mingw.
For that I load the source and drop the source to my directory.
Than I wrote a makefile. For Linux it runs perfectly. But for Win I got many errors - some of them I can't fix already:
$ make
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN -c -o main.o main.c
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN -c -o usleep.o usleep.c
------------------------------------------
clean up
make clean
make[1]: Betrete Verzeichnis '/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Verlasse Verzeichnis '/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Betrete Verzeichnis '/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Betrete Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: Erzeugen von libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Verlasse Verzeichnis '/mnt/prog/pthreads'
make[1]: Verlasse Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
------------------------------------------
create object files
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: Falsche Verschiebungsadresse 0x0 in Abschnitt `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Ungültiger Vorgang
collect2: ld returned 1 exit status
make: *** [prog] Fehler 1
How can I fix the linking problem?
to Dayal rai
the includes of my headers are (in the order, which I think they are called):
main.c
/*#include <errno.h> */
/*#include <fcntl.h> */
#include <inttypes.h>
#include "./pthreads/pthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <termios.h> */
/*#include <unistd.h> */
#include "usleep.h" /* um usleep nutzen zu können */
usleep.h
...
#include "unistd.h"
#include <inttypes.h>
#include <stdio.h>
...
Could it be that it is wrong to include unistd.h for a windows target?
I additionally reduce the number of headers and change their orders in any possibility, but the error still exist. I don't know where it come from....
main.c
#include <inttypes.h> /* Spezielle int-Datentypen */
#include <stdio.h>
#if defined(FORCEWIN) || defined(_WIN64) || defined(_WIN32)
#include <time.h>
#include <winsock2.h>
#elif __unix /* all unices not caught above */
#include <sys/time.h>
#include <sys/socket.h>
#endif
usleep.h
with english feedback
LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1
I have remove -DPTW32_STATIC_LIB the from the CFLAGS and now I get this
LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/4.6/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../../src/configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/include' --mandir='/usr/share/man' --infodir='/usr/share/info' --sysconfdir=/etc --localstatedir=/var --libexecdir='/usr/lib/gcc-mingw-w64' --disable-maintainer-mode --disable-dependency-tracking --prefix=/usr --enable-shared --enable-static --disable-multilib --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --libdir=/usr/lib --enable-libstdcxx-time=yes --with-tune=generic --enable-version-specific-runtime-libs --enable-threads=win32 --enable-fully-dynamic-string --enable-sjlj-exceptions --enable-languages=c,c++,fortran,objc,obj-c++,ada --enable-lto --with-plugin-ld --target=x86_64-w64-mingw32 --with-gxx-include-dir=/usr/include/c++/4.6 --with-as=/usr/bin/x86_64-w64-mingw32-as --with-ld=/usr/bin/x86_64-w64-mingw32-ld
Thread model: win32
gcc version 4.6.3 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/
COLLECT_GCC_OPTIONS='-o' 'possix.exe' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-w64-mingw32/4.6/collect2 -m i386pep -Bdynamic -o possix.exe /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/4.6 -L/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib ./main.o ./usleep.o ./pthreads/pthread.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtend.o
./main.o:main.c:(.text+0x1c): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x23): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x6b): undefined reference to `__imp_pthread_exit'
./main.o:main.c:(.text+0x8b): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x9e): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x19c): undefined reference to `__imp_pthread_exit'
/usr/bin/x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1
It looks to me like a error coming from pthread confused
In reference to the comments by Gavin Smith below I have done some changes in the makefile:
$ LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -g -O3 -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a -o possix.exe -v
GNU ld (GNU Binutils) 2.22.90.20120919
./main.o: In function `thr_ausgabe':
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:72: undefined reference to `printf'
/mnt/prog/./main.c:83: undefined reference to `__imp_pthread_exit'
./main.o: In function `thr_arbeite':
/mnt/prog/./main.c:44: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:46: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:56: undefined reference to `__imp_pthread_exit'
./main.o: In function `main':
/mnt/prog/./main.c:89: undefined reference to `__main'
/mnt/prog/./main.c:100: undefined reference to `__imp_pthread_mutex_init'
/mnt/prog/./main.c:105: undefined reference to `__imp_pthread_create'
/mnt/prog/./main.c:116: undefined reference to `__imp_pthread_join'
/mnt/prog/./main.c:111: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:111: undefined reference to `fprintf'
/mnt/prog/./main.c:106: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:106: undefined reference to `fprintf'
./usleep.o: In function `msleep':
/mnt/prog/./usleep.c:16: undefined reference to `__imp_select'
./usleep.o: In function `usleep':
/mnt/prog/./usleep.c:33: undefined reference to `__imp_select'
make: *** [prog] Error 1
The symbols of the main are now:
$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_frame
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_loc
0000000000000000 N .debug_ranges
U fprintf
U __imp___iob_func
U __imp_pthread_create
U __imp_pthread_exit
U __imp_pthread_join
U __imp_pthread_mutex_init
U __imp_pthread_mutex_lock
U __imp_pthread_mutex_unlock
0000000000000000 T main
U __main
U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
U printf
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup
I looking in the objectfile of the lib too an found the symbols a bit different to the main:
000000000000a9c0 T pthread_create
000000000000e6a0 T pthread_exit
000000000000b510 T pthread_join
00000000000024b0 T pthread_mutex_init
0000000000006c70 T pthread_mutex_lock
0000000000005800 T pthread_mutex_unlock
could it be useful to strip or rename the symbols from object files?
The actual situation:
terminal:
$ LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog/win'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog/win'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
usleep.o main.o
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/win/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/win/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/win/pthreads'
make[1]: Leaving directory `/mnt/prog/win/pthreads'
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a -o possix.exe
./main.o:main.c:(.text+0x33): undefined reference to `printf'
./main.o:main.c:(.text.startup+0x6): undefined reference to `__main'
x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x6 in section `.text.startup'
x86_64-w64-mingw32-ld: final link failed: Invalid operation
make: *** [prog] Error 1
the symbols of the object file
$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
U fprintf
U __imp___iob_func
0000000000000000 T main
U __main
U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
U printf
U pthread_create
U pthread_exit
U pthread_join
U pthread_mutex_init
U pthread_mutex_lock
U pthread_mutex_unlock
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup
I really don't understand why he can't find printf and main....
usleep.o
$ x86_64-w64-mingw32-nm usleep.o
0000000000000000 b .bss
0000000000000000 d .data
U __imp_select
0000000000000000 T msleep
0000000000000000 p .pdata
0000000000000000 t .text
0000000000000050 T usleep
0000000000000000 r .xdata
The error:
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
means that the symbol __imp_select is not defined. It could be in some other C file that you did not include in your final link step.
It looks like you have created a static library file libpthreadGC2.a, but your linking command x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe -v only links three object files and it is not among them. Try adding -lpthreadGC2 or libpthreadGC2.a to the linking command.
If you want to check if an object file contains a missing symbol (e.g. __imp_pthread_mutex_unlock), you can try running nm libpthreadGC2.a | grep '__imp_pthread_mutex_unlock' to see if it is there.
I read in the README for the pthread sources you linked to that you have to define PTW32_STATIC_LIB when building - is there a reason you removed the -DPTW32_STATIC_LIB option?

Resources