0

I have the following code... which is throwing a main.cpp:18: undefined reference to phrase_init() Coming from C# experience C is a bit annoying especially when trying to figure out where the problem is :( Any idea or pointer will be greatly appreciated!

phrase.h:

#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;
}

main.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
9
  • 5
    Need a few pointers? (Sorry, couldn't help myself) xkcd.com/138 Dec 14, 2011 at 3:06
  • 2
    There's a good chance you're compiling/linking it wrong, show us the command you use to compile/link the program.
    – AusCBloke
    Dec 14, 2011 at 3:07
  • Can you paste your compile and link script?
    – Louis
    Dec 14, 2011 at 3:07
  • Uhm... don't know. I am using NetBeans C++ 7. Like I said... c# background with VS2010 as tool of choice. Not familiar with Linux VI, making makefiles... etc. (Yet)
    – bleepzter
    Dec 14, 2011 at 3:10
  • 1
    Like the other commenters have said, it's probably the way you're compiling. It sounds like you're trying to compile main.c without Phrase.c
    – Kevin
    Dec 14, 2011 at 3:12

2 Answers 2

5

You have to tell the linker where to find the symbol.

For example:

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"

Or, to do all of the above in one command:

g++ phrase.cpp main.cpp -o main

Note that I'm assuming both source files are C++, and are named with .cpp extensions. Your question refers to files with both .c and .cpp extensions, and the question is tagged .

If you're using C, then you shouldn't be using C++-specific headers in main.c, and you should use gcc rather than g++.

If you're using C++, all your source files should have .cpp suffixes and you should used g++.

And if you're trying to mix C and C++, then you should be using extern "C" so the compiler knows what you're doing (but it's usually easier to use pure C++).

2
  • Ok, thanks! I just realized that it is the type of project in NetBeans that is making all the mashup. By default it creates a main.cpp, and is thinking the code is in c++.
    – bleepzter
    Dec 14, 2011 at 3:14
  • 1
    @bleepzter: It is C++; there's no <iostream> in C.
    – AusCBloke
    Dec 14, 2011 at 3:15
1

And you really should compile with -Wall -g so add to your Makefile

CXXFLAGS+= -Wall -g
CFLAGS+= -Wall -g

the first is for C++, the second is for C

I also think you should write by hand your own (simpler) Makefile for make (instead of copying a more complex Makefile that you don't understand).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.