【发布时间】:2021-02-18 03:38:51
【问题描述】:
我不想使用大型 IDE,而是想尝试使用 makefile 的第一次体验。在不太了解 makefile 的情况下,我找到了一个示例并尝试将其调整到我的项目中。 生成文件如下所示:
#This is an easier to use and modify makefile, but it is slightly more difficult to read than the simple one:
#
# 'make depend' uses makedepend to automatically generate dependencies
# (dependencies are added to end of Makefile)
# 'make' build executable file 'mycc'
# 'make clean' removes all .o and executable files
#
# define the C compiler to use
#CC = gcc
#CC = "C:\MinGW\bin\g++.exe"
CXX = "C:\MinGW\bin\g++.exe"
# define any compile-time flags
#CPPFLAGS = -Wall -g -std=c++11
CXXFLAGS = -Wall -g -std=c++11
# define any directories containing header files other than /usr/include
#
#INCLUDES = -I/home/newhall/include -I../include
INCLUDES = -IC:\Users\k0cke\Desktop\TestMake\ATCCalib\inc
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
#LFLAGS = -L/home/newhall/lib -L../lib
LFLAGS = -LC:\Users\k0cke\Desktop\TestMake\ATCCalib\lib
# define any libraries to link into executable:
# if I want to link in libraries (libx.so or libx.a) I use the -llibname
# option, something like (this will link in libmylib.so and libm.so:
#LIBS = -lmylib -lm
LIBS = -lvisa32
# define the C source files
#SRCS = emitter.c error.c init.c lexer.c main.c symbol.c parser.c
SRCS = main.cpp atcFlash.cpp measurements.cpp SerialCommunicationClass.cpp
# define the C object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)
# define the executable file
#MAIN = mycc
MAIN = apdCalib.exe
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
.PHONY: depend clean
all: $(MAIN)
@echo Simple compiler named mycc has been compiled
$(MAIN): $(OBJS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
# (see the gnu make manual section about automatic variables)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
$(RM) *.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# DO NOT DELETE THIS LINE -- make depend needs it
我将 mingw 安装到“C\MinGW\bin”。在文件夹“C:\Users\k0cke\Desktop\TestMake\ATCCalib”中,我有四个 .cpp 源文件和“makefile”。在“C:\Users\k0cke\Desktop\TestMake\ATCCalib\inc”中,我有 6 个 .h 文件。 “visa.h”和“visatype.h”是头文件,应该允许我使用visa32.lib的功能。 “C:\Users\k0cke\Desktop\TestMake\ATCCalib\lib”包含库“visa32.lib”。
在 Windows cmd 窗口中,导航到“C:\Users\k0cke\Desktop\TestMake\ATCCalib”并输入 mingw32-make。
现在我收到链接器错误
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\k0cke\AppData\Local\Temp\ccPAAw9m.o:measurements.cpp:(.text+0x10): undefined reference to `viOpenDefaultRM@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\k0cke\AppData\Local\Temp\ccPAAw9m.o:measurements.cpp:(.text+0x6d): undefined reference to `viOpen@20'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\k0cke\AppData\Local\Temp\ccPAAw9m.o:measurements.cpp:(.text+0x93): undefined reference to `viGetAttribute@12'
.
.
.
在“measurements.cpp”中,我#include "measurement.h"。在“measurement.h”中,我#include "visa.h"和#include "visatype.h"
你有什么建议我会犯什么错误吗?我似乎找不到任何东西。
编辑:以下是控制台中显示的构建命令:
"C:\MinGW\bin\g++.exe" -Wall -g -std=c++11 -IC:\Users\k0cke\Desktop\TestMake\ATCCalib\inc -o apdCalib.exe main.cpp atcFlash.cpp measurements.cpp SerialCommunicationClass.cpp -LC:\Users\k0cke\Desktop\TestMake\ATCCalib\lib -lvisa32
atcFlash.cpp: In function 'void atcFlash(serialCommunication&, std::string, std::string, std::string)':
atcFlash.cpp:23:6: warning: unused variable 'chooseSector' [-Wunused-variable]
23 | int chooseSector = 0;
| ^~~~~~~~~~~~
atcFlash.cpp:24:6: warning: unused variable 'validSector' [-Wunused-variable]
24 | int validSector = 0;
| ^~~~~~~~~~~
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\k0cke\AppData\Local\Temp\ccLOPMRY.o: in function `Z9measureACiRdNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE':
C:\Users\k0cke\Desktop\TestMake\ATCCalib/measurements.cpp:12: undefined reference to `viOpenDefaultRM@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\k0cke\Desktop\TestMake\ATCCalib/measurements.cpp:21: undefined reference to `viOpen@20'
.
.
【问题讨论】:
-
嗨@TedLyngmo,您是在谈论错误代码吗?完整的编译器和链接器命令在 makefile-code 中。
-
好的,我添加了命令。
-
好吧好吧。如果您查看
visa.h,是否所有函数都像这样包围#ifdef __cplusplusextern "C" {#endif...所有函数...#ifdef __cplusplus}#endif? -
是的,文件开头写着
#if defined(__cplusplus) || defined(__cplusplus__) extern "C" { #endif,最后写着#if defined(__cplusplus) || defined(__cplusplus__) } #endif -
感谢您的帮助。在 makefile 中,我将行更改为
CXX = "C:\MinGW\bin\mingw32-g++.exe"。但是,链接器错误仍然存在。