首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mingw32无法链接到库

mingw32无法链接到库
EN

Stack Overflow用户
提问于 2016-09-07 19:08:16
回答 2查看 900关注 0票数 0

好的,这是一个有趣的测试,已经测试了两个版本的明明威*,这可能是用户错误,但谷歌辜负了我。

我仍然不能正确地构建更复杂的项目,即使它们应该,例如--让我们以牛顿物理引擎sdk中的例子为例,它是一个相当简单的程序。

首先,我们使用包含的makefile构建newton (因为cmake不断输出坏的makefile),它输出了一个非常合理的.a文件,这是维护人员特别推荐的。

这些是牛顿马卡菲里的旗子

代码语言:javascript
复制
-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)

但是,如果我想要实际构建这个示例,那么我们就会遇到一些小问题。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
#*******************************************************
#
# 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的本意,但它破坏了即使是最简单的测试用例也意味着出了问题)。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-07 19:14:28

你的问题很简单。

-c开关的gcc只做编译步骤,不链接和产生一个可执行模块。

指示gcc将输出写入具有.exe扩展名的文件对gcc没有特殊意义。

因此,您只是试图执行对象文件,所以Winodws会抱怨。

我假设您在显示的命令中输入了最后一个参数,它们应该读为hello_world.c

票数 1
EN

Stack Overflow用户

发布于 2016-09-19 03:22:32

好的,我终于缩小了不能编译或链接任何东西的根本原因。

原因是我已经成功链接的两个库都是为了与C++98兼容而编写的--这两个库的构建和链接都很好。然而,我的框架也使用过牛顿动力学和box2D,这是我移植过程中遇到的问题,它们都依赖于C++11或更高版本。

这花了一段时间才发现,因为我从来没有想到这个问题可能只是工具链的C++11子集,当我注意到我甚至不能构建最简单的c++11项目(仅使用nullptr的项目)时,我注意到它完全是巧合的,如果我使用的是g++ 4.5或更高版本--这是预期的,但从4.6甚至更高版本就可以使用了。

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

代码语言:javascript
复制
#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

然后我们就会有很多错误

即:

代码语言:javascript
复制
||=== 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做了什么(尽管这不是根本原因),仍然给serge一个确定的答案。

也许这就是mingw设计的工作方式,但后来它被设计打破了(然而,我怀疑他们没有理由故意破坏C++11)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39377428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档