mingw32无法链接到库

时间:2016-09-07 19:08:16

标签: c++ windows makefile linker-errors

好吧,所以这是一个有趣的,已经在mingw *的两个版本上测试过,它可能是用户错误但谷歌让我失望。

我仍然无法正确地构建更复杂的项目,即使它们应该 - 例如 - 让我们采用牛顿物理引擎sdk中包含的示例,它是一个非常简单的程序。 / p>

首先我们使用包含的makefile构建newton(由于维护者特别推荐,因为cmake不断输出损坏的makefile),这很简单 - 它输出一个完全合理的.a文件。

这些是牛顿makefile中的标志

-c -Wall -Wno-strict-aliasing  -DPTW32_BUILD -DPTW32_STATIC_LIB -D_NEWTON_STATIC_LIB -D_MINGW_32_VER -m32 -O2 -fpic -g -msse -msse3 -msse4 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant -I$(DG_INCLUDED_PATH) -I$(DG_INCLUDED_PHYSICS_PATH) -I$(DG_INCLUDED_MESH_PATH) -I$(DG_INCLUDED_OPENCL_PATH)

但是,如果我想真正构建示例,那么我们会遇到一些小问题。

g++ -march=core2 -o out.exe ex.cpp -g -m32 -lNewton 2>&1 | tee build.log

我们得到了什么?链接器错误。未定义的引用无处不在,因为名称mangling似乎与

不匹配

我可能只是忽略了一些显而易见的事情,但我对于那些显而易见的事情感到迷茫。

* g ++。exe(TDM-2 mingw32)4.4.1和g ++。exe(GCC)5.3.0 所以这将是TDM mingw g ++ 4.4.1和vanilla mingw 5.3.0

这里是makefile(您可能会注意到默认mingw32 makefile对makefile的一些小调整,这是因为在我修复了一些空白问题之前它根本没有构建):

#*******************************************************
#
# Newton game dynamics 
# copy right by Julio Jerez 2002 - 2012
#
#*******************************************************
#
# Generic makefile 
# this make file generate the libraries: 
# dg, physics, and newton
#
#*******************************************************  


# ******************************************************
#
# dg low level library
#
# ******************************************************
DG_INCLUDED_PATH = ../../source/core
DG_PATH = $(DG_INCLUDED_PATH)/
DG_SRCS = \
    $(DG_PATH)dgAABBPolygonSoup.cpp \
    $(DG_PATH)dgAsyncThread.cpp \
    $(DG_PATH)dgConvexHull3d.cpp \
    $(DG_PATH)dgConvexHull4d.cpp \
    $(DG_PATH)dg.cpp \
    $(DG_PATH)dgCRC.cpp \
    $(DG_PATH)dgDebug.cpp \
    $(DG_PATH)dgDelaunayTetrahedralization.cpp \
    $(DG_PATH)dgGeneralMatrix.cpp \
    $(DG_PATH)dgGeneralVector.cpp \
    $(DG_PATH)dgGoogol.cpp \
    $(DG_PATH)dgIntersections.cpp \
    $(DG_PATH)dgMatrix.cpp \
    $(DG_PATH)dgMemory.cpp \
    $(DG_PATH)dgMutexThread.cpp \
    $(DG_PATH)dgNode.cpp \
    $(DG_PATH)dgPolygonSoupBuilder.cpp \
    $(DG_PATH)dgPolyhedra.cpp \
    $(DG_PATH)dgPolyhedraMassProperties.cpp \
    $(DG_PATH)dgQuaternion.cpp \
    $(DG_PATH)dgRandom.cpp \
    $(DG_PATH)dgRefCounter.cpp \
    $(DG_PATH)dgRef.cpp \
    $(DG_PATH)dgSmallDeterminant.cpp \
    $(DG_PATH)dgSPDMatrix.cpp \
    $(DG_PATH)dgObb.cpp \
    $(DG_PATH)dgThread.cpp \
    $(DG_PATH)dgThreadHive.cpp \
    $(DG_PATH)dgTree.cpp \
    $(DG_PATH)dgTypes.cpp


# ******************************************************
#
# Physics engine files
#
# ******************************************************
DG_INCLUDED_PHYSICS_PATH = ../../source/physics
DG_PHYSICS_PATH = $(DG_INCLUDED_PHYSICS_PATH)/
DG_PHYSICS_SRCS = \
    $(DG_PHYSICS_PATH)dgBody.cpp \
    $(DG_PHYSICS_PATH)dgDynamicBody.cpp \
    $(DG_PHYSICS_PATH)dgKinematicBody.cpp \
    $(DG_PHYSICS_PATH)dgBallConstraint.cpp \
    $(DG_PHYSICS_PATH)dgBilateralConstraint.cpp \
    $(DG_PHYSICS_PATH)dgBody.cpp \
    $(DG_PHYSICS_PATH)dgDynamicBody.cpp \
    $(DG_PHYSICS_PATH)dgKinematicBody.cpp \
    $(DG_PHYSICS_PATH)dgBodyMasterList.cpp \
    $(DG_PHYSICS_PATH)dgBroadPhase.cpp \
    $(DG_PHYSICS_PATH)dgCollisionBox.cpp \
    $(DG_PHYSICS_PATH)dgCollisionBVH.cpp \
    $(DG_PHYSICS_PATH)dgCollisionCapsule.cpp \
    $(DG_PHYSICS_PATH)dgCollisionChamferCylinder.cpp \
    $(DG_PHYSICS_PATH)dgCollisionCompoundFractured.cpp \
    $(DG_PHYSICS_PATH)dgCollisionCompound.cpp \
    $(DG_PHYSICS_PATH)dgCollisionCone.cpp \
    $(DG_PHYSICS_PATH)dgCollisionConvex.cpp \
    $(DG_PHYSICS_PATH)dgCollisionConvexHull.cpp \
    $(DG_PHYSICS_PATH)dgCollisionConvexPolygon.cpp \
    $(DG_PHYSICS_PATH)dgCollision.cpp \
    $(DG_PHYSICS_PATH)dgCollisionCylinder.cpp \
    $(DG_PHYSICS_PATH)dgCollisionDeformableClothPatch.cpp \
    $(DG_PHYSICS_PATH)dgCollisionDeformableSolidMesh.cpp \
    $(DG_PHYSICS_PATH)dgCollisionDeformableMesh.cpp \
    $(DG_PHYSICS_PATH)dgCollisionHeightField.cpp \
    $(DG_PHYSICS_PATH)dgCollisionInstance.cpp \
    $(DG_PHYSICS_PATH)dgCollisionMesh.cpp \
    $(DG_PHYSICS_PATH)dgCollisionNull.cpp \
    $(DG_PHYSICS_PATH)dgCollisionScene.cpp \
    $(DG_PHYSICS_PATH)dgCollisionSphere.cpp \
    $(DG_PHYSICS_PATH)dgCollisionTaperedCapsule.cpp \
    $(DG_PHYSICS_PATH)dgCollisionTaperedCylinder.cpp \
    $(DG_PHYSICS_PATH)dgCollisionUserMesh.cpp \
    $(DG_PHYSICS_PATH)dgConstraint.cpp \
    $(DG_PHYSICS_PATH)dgContact.cpp \
    $(DG_PHYSICS_PATH)dgCorkscrewConstraint.cpp \
    $(DG_PHYSICS_PATH)dgDeformableBody.cpp \
    $(DG_PHYSICS_PATH)dgDeformableContact.cpp \
    $(DG_PHYSICS_PATH)dgHingeConstraint.cpp \
    $(DG_PHYSICS_PATH)dgNarrowPhaseCollision.cpp \
    $(DG_PHYSICS_PATH)dgSlidingConstraint.cpp \
    $(DG_PHYSICS_PATH)dgUniversalConstraint.cpp \
    $(DG_PHYSICS_PATH)dgUpVectorConstraint.cpp \
    $(DG_PHYSICS_PATH)dgUserConstraint.cpp \
    $(DG_PHYSICS_PATH)dgWorld.cpp \
    $(DG_PHYSICS_PATH)dgDeformableBodiesUpdate.cpp \
    $(DG_PHYSICS_PATH)dgWorldDynamicsParallelSolver.cpp \
    $(DG_PHYSICS_PATH)dgWorldDynamicsSimpleSolver.cpp \
    $(DG_PHYSICS_PATH)dgWorldDynamicUpdate.cpp


# ******************************************************
#
# mesh gemotry 
#
# ******************************************************
DG_INCLUDED_MESH_PATH = ../../source/meshUtil
DG_MESH_PATH = $(DG_INCLUDED_MESH_PATH)/
DG_MESH_SRCS = \
    $(DG_MESH_PATH)dgMeshEffect1.cpp \
    $(DG_MESH_PATH)dgMeshEffect2.cpp \
    $(DG_MESH_PATH)dgMeshEffect3.cpp \
    $(DG_MESH_PATH)dgMeshEffect4.cpp \
    $(DG_MESH_PATH)dgMeshEffect5.cpp \
    $(DG_MESH_PATH)dgMeshEffect6.cpp 

# ******************************************************
#
# open cl 
#
# ******************************************************
DG_INCLUDED_OPENCL_PATH = ../../source/openCL
DG_OPENCL_PATH = $(DG_INCLUDED_OPENCL_PATH)/
#DG_OPENCL_SRCS = \
#   $(DG_OPENCL_PATH)dgOpencl.cpp \
#   $(DG_OPENCL_PATH)dgOpenclInstance.cpp \
#   $(DG_OPENCL_PATH)dgOpenclBroadPhase.cpp



# ******************************************************
#
# Newton engine files
#g++ -shared -o libNewton.dll libNewton.a
# ******************************************************
DG_INCLUDED_NEWTON_PATH = ../../source/newton
DG_NEWTON_PATH = $(DG_INCLUDED_NEWTON_PATH)/
DG_NEWTON_SRCS = \
    $(DG_NEWTON_PATH)Newton.cpp \
    $(DG_NEWTON_PATH)NewtonClass.cpp

# ******************************************************
#
# Allsource files
#
# ******************************************************
ALL_SRC_FILES = $(DG_SRCS) $(DG_PHYSICS_SRCS) $(DG_OPENCL_SRCS) $(DG_MESH_SRCS) $(DG_NEWTON_SRCS)
DG_OBJ_FILES = $(ALL_SRC_FILES:.cpp=.o)

COMPILER = gcc

# mingw options  gcc 4.4.2
#CPU_FLAGS = -m32 -O0 -fPIC -g -msse -msse2 -mfpmath=sse 
#CPU_FLAGS = -m32 -O0 -fPIC -g -msse -msse4.1 -mfpmath=sse 
#CPU_FLAGS = -m32 -O2 -fpic -g -msse -msse4.1 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant
CPU_FLAGS = -m32 -O2 -fpic -g -msse -msse3 -msse4 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant

FLAGS  = -Wall -Wno-strict-aliasing  -DPTW32_BUILD -DPTW32_STATIC_LIB -D_NEWTON_STATIC_LIB -D_MINGW_32_VER $(CPU_FLAGS) -I$(DG_INCLUDED_PATH) -I$(DG_INCLUDED_PHYSICS_PATH) -I$(DG_INCLUDED_MESH_PATH) -I$(DG_INCLUDED_OPENCL_PATH)

.SUFFIXES : .o .cpp
.cpp.o :
    $(COMPILER) $(FLAGS) -o $@ $<

# main target
engine : libNewton.a


# clean all objects target
clean :
    rm $(DG_OBJ_FILES)
    touch $(ALL_SRC_FILES)

# libraries targets
libNewton.a : $(DG_OBJ_FILES)
    ar r $@ $?
    strip -g -S -d -v libNewton.a -o libNewton.a
    cp libNewton.a ../../../packages/mingw32/libNewton.a
    cp ../../source/newton/Newton.h ../../../packages/mingw32/Newton.h
#   $(COMPILER) -shared -Wl,-soname,libNewton.dll $? -o libNewton.dll 
#   cp libNewton.dll ../../../packages/mingw32/libNewton.dll
    #sudo cp libNewton.dll /usr/lib

如果我在没有-c的情况下构建我得到的链接器错误(所有的牛顿功能都是未定义的,这对任何人来说都不是一个惊喜,因为这是-c的意思,但事实是它打破了即使是最简单的测试用例也意味着有些事情是错误的)

C:\Users\Thunder\AppData\Local\Temp\cchfxWSh.o: In function `Z20CreateBackgroundBodyP11NewtonWorld':
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:17: undefined reference to `_imp__NewtonCreateTreeCollision'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:20: undefined reference to `_imp__NewtonTreeCollisionBeginBuild'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:23: undefined reference to `_imp__NewtonTreeCollisionAddFace'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:26: undefined reference to `_imp__NewtonTreeCollisionEndBuild'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:29: undefined reference to `dGetIdentityMatrix()'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:30: undefined reference to `_imp__NewtonCreateDynamicBody'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:33: undefined reference to `_imp__NewtonDestroyCollision'
C:\Users\Thunder\AppData\Local\Temp\cchfxWSh.o: In function `ApplyGravity':
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:45: undefined reference to `_imp__NewtonBodyGetMassMatrix'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:47: undefined reference to `_imp__NewtonBodySetForce'
C:\Users\Thunder\AppData\Local\Temp\cchfxWSh.o: In function `Z18CreateFreeFallBallP11NewtonWorld':
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:53: undefined reference to `_imp__NewtonCreateSphere'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:56: undefined reference to `dGetIdentityMatrix()'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:58: undefined reference to `_imp__NewtonCreateDynamicBody'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:61: undefined reference to `_imp__NewtonBodySetForceAndTorqueCallback'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:65: undefined reference to `_imp__NewtonBodySetMassProperties'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:68: undefined reference to `_imp__NewtonBodySetLinearDamping'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:71: undefined reference to `_imp__NewtonDestroyCollision'
C:\Users\Thunder\AppData\Local\Temp\cchfxWSh.o: In function `main':
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:79: undefined reference to `_imp__NewtonCreate'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:86: undefined reference to `_imp__NewtonInvalidateCache'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:90: undefined reference to `_imp__NewtonUpdate'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:93: undefined reference to `_imp__NewtonBodyGetMatrix'
C:\Users\Thunder\Documents\projects\libproto framework\src/ex.cpp:98: undefined reference to `_imp__NewtonDestroy'
collect2.exe: error: ld returned 1 exit status

2 个答案:

答案 0 :(得分:1)

你的问题很简单。

-c的{​​{1}}开关只进行编译步骤而不链接并生成可执行模块。

您指示gcc将输出写入gcc扩展名的文件这一事实对.exe没有任何特殊意义。

所以你只是试图执行目标文件,因此Winodws抱怨。

<小时/> 我假设你错误输入了显示的命令中的最后一个参数,它们应该显示为gcc

答案 1 :(得分:0)

好吧,我最终缩小了无法编译或链接任何内容的根本原因。

原因是我已成功链接的两个库被编写为C ++ 98兼容 - 这些构建和链接很好。 然而,我的框架也使用的牛顿动力学和box2D [这是我在移植时遇到的麻烦]都依赖于C ++ 11或更高版本。

这需要一段时间才能找到,因为我从来没有想到问题可能只出现在工具链的C ++ 11子集中,当我注意到我无法建立甚至是最简单的c ++ 11项目(仅使用nullptr的项目),如果我使用g ++ 4.5或更早版本,这将是预期的,但它自4.6及以后就已经可用。

因此我们进行了故障排除,我采用了hello world应用程序(非常标准但无论如何包含代码)并使用

构建它
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

g ++。exe -Wall -fexceptions -g -O2 -c gcc530_sanitytest \ main.cpp -o obj \ Debug \ main.o

这非常有效。

如果我们再尝试

g ++。exe -Wall -fexceptions -std = c ++ 11 -g -O2 -c gcc530_sanitytest \ main.cpp -o obj \ Debug \ main.o

然后我们得到了一大堆错误

即:

||=== Build: Debug in gcc530_sanitytest (compiler: GNU GCC Compiler) ===|
c:\mingw\include\sys\stat.h|173|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: '_ino_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: '_mode_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: '_off_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|173|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: '_ino_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: '_mode_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: '_off_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|180|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: '_ino_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: '_mode_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: '__off64_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|188|error: 'time_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '_ino_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '_mode_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '_dev_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '__off64_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '__time64_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '__time64_t' does not name a type|
c:\mingw\include\sys\stat.h|195|error: '__time64_t' does not name a type|
c:\mingw\include\io.h|335|error: 'time_t' does not name a type|
c:\mingw\include\io.h|335|error: 'time_t' does not name a type|
c:\mingw\include\io.h|335|error: 'time_t' does not name a type|
c:\mingw\include\io.h|336|error: 'time_t' does not name a type|
c:\mingw\include\io.h|336|error: 'time_t' does not name a type|
c:\mingw\include\io.h|336|error: 'time_t' does not name a type|
c:\mingw\include\io.h|362|error: '__time64_t' does not name a type|
c:\mingw\include\io.h|362|error: '__time64_t' does not name a type|
c:\mingw\include\io.h|362|error: '__time64_t' does not name a type|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|146|error: '::fwide' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|153|error: '::mbsinit' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|198|error: '::wmemcmp' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|199|error: '::wmemcpy' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|200|error: '::wmemmove' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|201|error: '::wmemset' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|208|error: '::wmemchr' has not been declared|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar||In function 'wchar_t* std::wmemchr(wchar_t*, wchar_t, std::size_t)':|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|229|error: invalid conversion from 'const wchar_t*' to 'wchar_t*' [-fpermissive]|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\cwchar|228|note:   initializing argument 1 of 'wchar_t* std::wmemchr(wchar_t*, wchar_t, std::size_t)'|
c:\mingw\lib\gcc\mingw32\5.3.0\include\c++\bits\char_traits.h|353|error: 'wmemcmp' was not declared in this scope|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
因此,这导致了一个明显的结论,即构建环境被破坏了,就我而言,这个问题很好地回答了问题 - 由于提醒/教育我什么是-c,所以仍然给了哔叽确认的答案(虽然这不是根本原因)。

也许这就是mingw设计工作的方式,但后来它被设计破坏了(不知怎的,我怀疑它,但我没有理由为什么他们会故意破坏C ++ 11)