首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的引用未定义...我做错了什么?

C中的引用未定义...我做错了什么?
EN

Stack Overflow用户
提问于 2011-12-14 11:03:58
回答 2查看 1.1K关注 0票数 0

我有以下代码...抛出来自C# experience C的main.cpp:18: undefined reference to phrase_init()有点烦人,特别是在试图找出问题所在的时候:(任何想法或指针都将非常感谢!

Phase.h:

代码语言:javascript
复制
#ifndef PHRASE_H
#define PHRASE_H

typedef struct
{
    char* Word1;
    char* Word2;
} Phrase;

Phrase* phrase_init(void);

#endif

Phrase.c:

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

main.c

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

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-14 11:09:34

您必须告诉链接器在哪里可以找到符号。

例如:

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

或者,在一个命令中完成以上所有操作:

代码语言:javascript
复制
g++ phrase.cpp main.cpp -o main

请注意,我假设两个源文件都是C++,并且使用.cpp扩展名命名。您的问题涉及同时具有.c.cpp扩展名的文件,并且问题标记为c++

如果您使用的是C,那么就不应该在main.c中使用C++特定的头文件,而应该使用gcc而不是g++

如果你使用的是C++,那么你所有的源文件都应该有.cpp后缀,并且你应该使用g++

如果你想混合使用C和C++,那么就使用you should be using extern "C",这样编译器就会知道你在做什么(但使用纯C++通常更容易)。

票数 5
EN

Stack Overflow用户

发布于 2011-12-14 14:03:41

你真的应该用-Wall -g编译,所以添加到你的Makefile

代码语言:javascript
复制
CXXFLAGS+= -Wall -g
CFLAGS+= -Wall -g

第一个是C++的,第二个是C的

我还认为你应该为make手动编写自己的(更简单的) Makefile (而不是复制你不理解的更复杂的Makefile )。

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

https://stackoverflow.com/questions/8499078

复制
相关文章

相似问题

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