c中未定义的引用…我做错了什么?
Undefined reference in c... What am I doing wrong?
我有以下代码…这是抛出一个main.cpp:18: undefined reference to phrase_init()
来自C#
经验C有点烦人,特别是当试图找出问题在哪里:(任何想法或指针将非常感激!
#ifndef PHRASE_H
#define PHRASE_H
typedef struct
{
char* Word1;
char* Word2;
} Phrase;
Phrase* phrase_init(void);
#endif
<标题> Phrase.c: #include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "phrase.h"
Phrase* phrase_init()
{
Phrase* phrase = (Phrase *)malloc(sizeof(Phrase));
phrase->Word1 = (char *)malloc(sizeof(char));
phrase->Word2 = (char *)malloc(sizeof(char));
return phrase;
}
<标题> c #include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cctype>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "phrase.h"
int main(int argc, char** argv) {
char* word1 = "Testing";
char* word2 = "Word2";
Phrase* test = phrase_init();
strcpy(test->Word1, word1);
strcpy(test->Word2, word2);
printf("Word 1: %s", test->Word1);
printf("Word 2: %s", test->Word2);
scanf("%s", word1);
}
<标题> Makefile: #
# There exist several targets which are by default empty and which can be
# used for execution of your targets. These targets are usually executed
# before and after some main targets. They are:
#
# .build-pre: called before 'build' target
# .build-post: called after 'build' target
# .clean-pre: called before 'clean' target
# .clean-post: called after 'clean' target
# .clobber-pre: called before 'clobber' target
# .clobber-post: called after 'clobber' target
# .all-pre: called before 'all' target
# .all-post: called after 'all' target
# .help-pre: called before 'help' target
# .help-post: called after 'help' target
#
# Targets beginning with '.' are not intended to be called on their own.
#
# Main targets can be executed directly, and they are:
#
# build build a specific configuration
# clean remove built files from a configuration
# clobber remove all built files
# all build all configurations
# help print help mesage
#
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
# .help-impl are implemented in nbproject/makefile-impl.mk.
#
# Available make variables:
#
# CND_BASEDIR base directory for relative paths
# CND_DISTDIR default top distribution directory (build artifacts)
# CND_BUILDDIR default top build directory (object files, ...)
# CONF name of current configuration
# CND_PLATFORM_${CONF} platform name (current configuration)
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
#
# NOCDDL
# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
# build
build: .build-post
.build-pre:
# Add your pre 'build' code here...
.build-post: .build-impl
# Add your post 'build' code here...
# clean
clean: .clean-post
.clean-pre:
# Add your pre 'clean' code here...
.clean-post: .clean-impl
# Add your post 'clean' code here...
# clobber
clobber: .clobber-post
.clobber-pre:
# Add your pre 'clobber' code here...
.clobber-post: .clobber-impl
# Add your post 'clobber' code here...
# all
all: .all-post
.all-pre:
# Add your pre 'all' code here...
.all-post: .all-impl
# Add your post 'all' code here...
# build tests
build-tests: .build-tests-post
.build-tests-pre:
# Add your pre 'build-tests' code here...
.build-tests-post: .build-tests-impl
# Add your post 'build-tests' code here...
# run tests
test: .test-post
.test-pre:
# Add your pre 'test' code here...
.test-post: .test-impl
# Add your post 'test' code here...
# help
help: .help-post
.help-pre:
# Add your pre 'help' code here...
.help-post: .help-impl
# Add your post 'help' code here...
# include project implementation makefile
include nbproject/Makefile-impl.mk
# include project make variables
include nbproject/Makefile-variables.mk
标题>标题>标题>标题>
你必须告诉链接器在哪里可以找到这个符号。
例如:g++ -c phrase.cpp # creates phrase.o which defines phrase_init()
g++ -c main.cpp # creates main.o which refers to phrase_init()
g++ phrase.o main.o -o main # creates executable file "main"
或者,在一个命令中完成上述所有操作:
g++ phrase.cpp main.cpp -o main
请注意,我假设两个源文件都是c++,并以.cpp
扩展名命名。您的问题是指具有.c
和.cpp
扩展名的文件,并且问题标记为c++。
如果你使用的是C语言,那么你不应该在main.c
中使用特定于c++的头文件,你应该使用gcc
而不是g++
。
如果你使用c++,你所有的源文件应该有.cpp
后缀,你应该使用g++
。
如果你想混合使用C和c++,那么你应该使用extern "C"
,这样编译器就知道你在做什么(但通常使用纯c++更容易)。
你真的应该用-Wall -g
来编译所以把Makefile
添加到
CXXFLAGS+= -Wall -g
CFLAGS+= -Wall -g
第一个用于c++,第二个用于C
我也认为你应该手写你自己的(更简单的)Makefile
为make(而不是复制一个更复杂的Makefile
,你不理解)。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?