我的交叉编译makefile有什么问题?

时间:2018-04-03 22:03:20

标签: c gcc makefile cross-compiling mingw32

我花了几天时间试图在我的Windows机器上交叉编译一个c程序。代码在我的linux机器上运行正常,但我尝试在该机器上交叉编译时有太多错误。现在我使用的是Cygwin和Mingw32。

    CC = x86_64-w64-mingw32-gcc
    CFLAGS = -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi1 
    -mwindows `sdl-config --cflags --libs`

    OBJECTS = main.o fft.o draw.o table.o serial.o

    main : $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o main

    %.o : %.c
    $(CC) $(CFLAGS) -c $<

    clean:
    @echo Cleaning up...
    rm -fr *.o main
    @echo Done.

这是我的makefile在编辑之后的样子,我认为需要交叉编译它。但是我得到了许多未定义的引用错误。我意识到这个问题与我在SO上看过的其他几个问题非常相似,但我似乎无法找到我的错误。

 $ make
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c main.c
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c fft.c
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c draw.c
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c table.c
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 - 
 lftdi1 -mwindows `sdl-config --cflags --libs` -c serial.c
 x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lGL -lGLU -lm -lSDL_image - 
 lfftw3 - 
 lftdi1 -mwindows `sdl-config --cflags --libs` main.o fft.o draw.o table.o 
 serial.o -o main
/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
/usr/src/debug/mingw64-x86_64-runtime-5.0.3-1/crt/crt0_c.c:18: undefined reference to `WinMain'
fft.o:fft.c:(.text+0xa): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x1b): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x2c): undefined reference to `fftw_malloc'
fft.o:fft.c:(.text+0x4d): undefined reference to `fftw_plan_dft_r2c_1d'
fft.o:fft.c:(.text+0x73d): undefined reference to `fftw_execute'
draw.o:draw.c:(.text+0xb): undefined reference to `__imp_glShadeModel'
draw.o:draw.c:(.text+0x1e): undefined reference to `__imp_glClearColor'
draw.o:draw.c:(.text+0x43): undefined reference to `gluOrtho2D'
draw.o:draw.c:(.text+0x4e): undefined reference to `__imp_glDisable'
draw.o:draw.c:(.text+0x5e): undefined reference to `__imp_glEnable'
draw.o:draw.c:(.text+0x82): undefined reference to `__imp_glViewport'
draw.o:draw.c:(.text+0x8e): undefined reference to `__imp_glMatrixMode'
draw.o:draw.c:(.text+0x97): undefined reference to `__imp_glLoadIdentity'
draw.o:draw.c:(.text+0xbe): undefined reference to `gluOrtho2D'
draw.o:draw.c:(.text+0xeb): undefined reference to `SDL_Init'
draw.o:draw.c:(.text+0xf4): undefined reference to `SDL_GetVideoInfo'
draw.o:draw.c:(.text+0x11c): undefined reference to `SDL_GL_SetAttribute'
draw.o:draw.c:(.text+0x13a): undefined reference to `SDL_SetVideoMode'
draw.o:draw.c:(.text+0x171): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x1a1): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x1d1): undefined reference to `SDL_GetError'
draw.o:draw.c:(.text+0x244): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x24b): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x252): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x29f): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x415): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x42c): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x447): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x4ef): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x55b): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x574): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x580): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x5e4): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x602): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x638): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x648): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x664): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x6b8): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x6e8): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x6fb): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x717): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x76b): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x7b5): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0x80d): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x814): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0x935): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xa00): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xa07): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xa0e): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xb0b): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xc09): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xc10): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xc19): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xc20): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0xea7): undefined reference to `__imp_glClear'
draw.o:draw.c:(.text+0xeb2): undefined reference to `__imp_glMatrixMode'
draw.o:draw.c:(.text+0xeb8): undefined reference to `__imp_glLoadIdentity'
draw.o:draw.c:(.text+0xec7): undefined reference to `__imp_glTranslatef'
draw.o:draw.c:(.text+0xf48): undefined reference to `__imp_glBegin'
draw.o:draw.c:(.text+0xf72): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0xf8f): undefined reference to `__imp_glVertex2f'
draw.o:draw.c:(.text+0xfba): undefined reference to `__imp_glEnd'
draw.o:draw.c:(.text+0x1089): undefined reference to `SDL_GL_SwapBuffers'
draw.o:draw.c:(.text+0x10c3): undefined reference to `__imp_glColor3ub'
draw.o:draw.c:(.text+0x10d8): undefined reference to `SDL_GetKeyState'
draw.o:draw.c:(.text+0x133d): undefined reference to `SDL_WM_ToggleFullScreen'
draw.o:draw.c:(.text+0x1364): undefined reference to `SDL_PollEvent'
draw.o:draw.c:(.text+0x1385): undefined reference to `SDL_PollEvent'
draw.o:draw.c:(.text+0x13ab): undefined reference to `SDL_SetVideoMode'
draw.o:draw.c:(.text+0x13e1): undefined reference to `SDL_GetError'
serial.o:serial.c:(.text+0x11): undefined reference to `ftdi_init'
serial.o:serial.c:(.text+0x1f): undefined reference to `ftdi_set_interface'
serial.o:serial.c:(.text+0x32): undefined reference to `ftdi_usb_open'
serial.o:serial.c:(.text+0x45): undefined reference to `ftdi_set_baudrate'
serial.o:serial.c:(.text+0x94): undefined reference to `ftdi_get_error_string'
serial.o:serial.c:(.text+0xc7): undefined reference to `ftdi_get_error_string'
serial.o:serial.c:(.text+0x139): undefined reference to `ftdi_write_data'
serial.o:serial.c:(.text+0x233): undefined reference to `ftdi_write_data'
collect2: error: ld returned 1 exit status
make: *** [makefile:6: main] Error 1

2 个答案:

答案 0 :(得分:1)

当您在Linux中链接到SO时,例如使用libGL.so,您将直接链接共享对象libGL.a

但是在Windows中,当您链接DLL时,实际上是链接一个静态库libGL.lib(本机Windows中的-l*),每个导入的函数包含一个thunk。

实际上,共享库和静态库之间的链接器规则是不同的。使用链接器命令行中的顺序链接静态库。

TL; DR; 您必须将CFLAGS放在链接器命令的末尾。此外,最好将LDFLAGSCC = x86_64-w64-mingw32-gcc CFLAGS = -Wall -O2 `sdl-config --cflags` LDFLAGS = -lmingw32 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi1 -mwindows `sdl-config --libs` OBJECTS = main.o fft.o draw.o table.o serial.o main : $(OBJECTS) $(CC) -o $@ $(CFLAGS) $(OBJECTS) $(LDFLAGS) %.o : %.c $(CC) $(CFLAGS) -c $< 分开:

WinMain

关于-mwindows引用,这是因为您编写了WinMain():即构建Windows应用程序,即以main()而不是main()开头的应用程序。如果您想坚持使用-mconsole,可以改用apply plugin: 'org.junit.platform.gradle.plugin' buildscript { ext.kotlin_version = '1.2.31' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.0" } } dependencies { testCompile 'io.kotlintest:kotlintest-runner-junit5:3.0.2' }

答案 1 :(得分:0)

我将对象和cflags切换到顶部,但忘记切换对象和cflags

main : $(OBJECTS) $(CC) $(OBJECTS) $(CFLAGS) -o main

现在我只得到一个未定义的WinMain引用

$ make
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image - 
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c main.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image - 
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c fft.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image - 
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c draw.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image - 
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c table.c
x86_64-w64-mingw32-gcc -Wall -O2 -lmingw32 -lm -lGL -lGLU -lSDL_image - 
lfftw3 -lftdi1 -mwindows `sdl-config --cflags --libs` -c serial.c
x86_64-w64-mingw32-gcc main.o fft.o draw.o table.o serial.o -Wall -O2 - 
lmingw32 -lm -lGL -lGLU -lSDL_image -lfftw3 -lftdi1 -mwindows `sdl-config -- 
cflags --libs` -o main
/usr/x86_64-w64-mingw32/sys- 
root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function 
`main':
/usr/src/debug/mingw64-x86_64-runtime-5.0.3-1/crt/crt0_c.c:18: undefined 
reference to `WinMain'
collect2: error: ld returned 1 exit status
make: *** [makefile:6: main] Error 1