我正在处理一个R包,它包含在src文件夹下用C和C++编写的代码。当前,包编译并在Rstudio上工作,因为它遵循默认的目录结构。随着项目的构建,我希望能够在src下在子文件夹中组织我的代码。按照“编写R扩展名”(在子目录下编译)的指示,我创建了一个名为'test'(/src/test)的文件夹,该文件夹现在包含了我的所有文件(*.c、*.cpp、*.h),并对我的Makevars进行了如下修改-
SOURCES_C = $(wildcard test/*.c)
SOURCES_CPP = $(wildcard test/*.cpp)
PKG_CPPFLAGS= -I${R_HOME}/include -I.
PKG_LIBS = -L${R_HOME}/lib
CXX_STD = CXX11
OBJECTS =$(SOURCES_CPP:.cpp=.o) $(SOURCES_C:.c=.o)
all : $(SHLIB)
#PKG_CFLAGS= -Wall
clean : rm -f *.o我希望能够在这种状态下编译程序,其中C/C++文件位于src内部的子文件夹下。使用前面提到的Makevars ->,正在为所有C/CPP文件使用正确的标志和编译器从测试文件夹构建单独的对象文件。但是,与共享对象的build命令存在一些差异。这是在编译src/test下的文件时的日志,该日志失败时出现了一个未定义的符号错误。
gcc -std=gnu99 -shared -L/usr/local/lib64 -o BioCro.so test/BBox.o test/Climate.o test/Compound.o test/Grid.o test/LeafOptics.o test/Maths.o test/Normal.o test/Point3D.o test/Ray.o test/Triangle.o test/Vector3D.o test/runFastTracer.o test/Assigncropcent.o test/AuxBioCro.o test/AuxCropGro.o test/AuxMaizeGro.o test/AuxcaneGro.o test/Auxcropcent.o test/AuxwillowGro.o test/BioCro.o test/CalculateBiogeochem.o test/Calculate_Soil_Layer_Temperature.o test/CanA.o test/CanAC_3D.o test/Century.o test/Copy_CropCent_To_DayCent_Structure.o test/Copy_SoilWater_BioCro_To_CropCent.o test/CropGro.o test/CropGro_c.o test/Filling_BioCro_SoilStructure.o test/assignManagement.o test/c3CanA.o test/c3EvapoTrans.o test/c3photo.o test/c4photo.o test/caneGro.o test/createNULLc3tree.o test/cropcent.o test/dailywillow.o test/denitrify.o test/diffusiv.o test/eC4photo.o test/getIdirIdiff.o test/getsoilprop.o test/leachdly.o test/maizeGro.o test/methane.o test/microclimate_for_3Dcanopy.o test/nitrify.o test/nox_pulse.o test/pi_funcs.o test/printcropcentoutput.o test/test_mainC.o test/tgmodel.o test/tracegas.o test/update_3Dcanopy_structure.o test/wfps.o test/willowCent.o test/willowGro.o -L/usr/local/R-3.1.0/lib64/R/lib -L/usr/local/R-3.1.0/lib64/R/lib -lR
installing to /home/vashist1/R/x86_64-unknown-linux-gnu-library/3.1/BioCro/
** R
** data
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/home/vashist1/R/x86_64-unknown-linux-gnu-library/3.1/BioCro/libs/BioCro.so':
/home/vashist1/R/x86_64-unknown-linux-gnu-library/3.1/BioCro/libs/BioCro.so:
undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE
Error: loading failed与成功构建成功的日志相比!
g++ -shared -L/usr/local/lib64 -o BioCro.so Assigncropcent.o AuxBioCro.o AuxCropGro.o AuxMaizeGro.o AuxcaneGro.o Auxcropcent.o AuxwillowGro.o BBox.o BioCro.o CalculateBiogeochem.o Calculate_Soil_Layer_Temperature.o CanA.o CanAC_3D.o Century.o Climate.o Compound.o Copy_CropCent_To_DayCent_Structure.o Copy_SoilWater_BioCro_To_CropCent.o CropGro.o CropGro_c.o Filling_BioCro_SoilStructure.o Grid.o LeafOptics.o Maths.o Normal.o Point3D.o Ray.o Triangle.o Vector3D.o assignManagement.o c3CanA.o c3EvapoTrans.o c3photo.o c4photo.o caneGro.o createNULLc3tree.o cropcent.o dailywillow.o denitrify.o diffusiv.o eC4photo.o getIdirIdiff.o getsoilprop.o leachdly.o maizeGro.o methane.o microclimate_for_3Dcanopy.o nitrify.o nox_pulse.o pi_funcs.o printcropcentoutput.o runFastTracer.o test_mainC.o tgmodel.o tracegas.o update_3Dcanopy_structure.o wfps.o willowCent.o willowGro.o -L/usr/local/R-3.1.0/lib64/R/lib -lR
installing to /home/vashist1/R/x86_64-unknown-linux-gnu-library/3.1/BioCro/libs1)共享对象在默认条件下使用g++编译,而在子目录条件下编译器使用gcc。我能通过Makevars改变这种行为吗?
2)进一步研究发现,未定义的符号错误是由-L/-l标志固定的链接错误。但是,对于两个构建命令,-L标志都是相同的。在默认情况下,是否还有其他链接库未能链接?
发布于 2015-04-28 14:38:32
我遇到了同样的问题。将RSiena包的示例作为“编写R扩展部分”1.2.1.3节中的一个示例,我注意到该包仍然有一些.cpp文件,而不是在子目录中。因此,我在dummy.cpp中添加了一个src/文件,其内容如下:
void dummy (void)
{
}在此之后,正确地将g++用于链接步骤,并按预期创建了.so文件。
在我的例子中,我不需要像我在回答中第一次提到的那样更改Makevars文件。即使没有下面的更改(因此只有dummy.cpp文件出现在src/中)链接也是正确的。我会留下它,以防它可能帮助其他人(稍微)不同的设置。
并在.o文件中的$(OBJECTS)变量列表中添加相应的Makevars文件:
OBJECTS = $(SOURCES:.cpp=.o) dummy.ohttps://stackoverflow.com/questions/26951274
复制相似问题