Thread: Trouble linking with extern

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Trouble linking with extern

    I have these files
    Code:
    //simgroup.h
    #include <vector>
    #include "simCmd.h"
    vector<simCmd*> *cmdList1;
    
    //driver.C
    #include <vector>
    extern vector<simCmd*> *cmdList1;
    
    
    //Make command
    INCLUDE = -I/usr/include
    LIBRARYPATH = -L/usr/lib -L/usr/X11R6/lib/
    LIBRARIES = $(LIBRARYPATH) -lGL -lGLU -lglut -lm -lXi -lXmu -lpthread
    
    CC = g++
    CFLAGS = -O2 $(INCLUDE)
    
    SRCS = simGroup.C simCmd.C driver.C
    OBJS = $(SRCS:.C=.o)
    EXE = simStart
    
    all: $(EXE)
    
    clean:
            rm -rf core *~ *.o $(EXE)
    
    $(EXE): $(OBJS) $(JLLIBS)
            $(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBRARIES) $(FORMS_LIBS)
    
    .C.o:
            $(CC) $(CFLAGS) $< -c -o $@
    
    //error when calling make
    g++ -O2 -I/usr/include driver.C -c -o driver.o
    driver.C:12: syntax error before `*' token
    make: *** [driver.o] Error 1
    Line 12 is the "extern vector<simCmd*> *cmdList1;"

    If I add this to driver.C #include "simgroup.C", it compiles driver.o but problem happens when linking . Error is..
    Code:
    g++ -O2 -I/usr/include driver.C -c -o driver.o
    g++ -O2 -I/usr/include -o simStart simGroup.o simCmd.o driver.o -L/usr/lib -L/usr/X11R6/lib/ -lGL -lGLU -lglut -lm -lXi -lXmu -lpthread
    driver.o(.text+0x52): In function `main':
    : undefined reference to `cmdList1'
    collect2: ld returned 1 exit status
    make: *** [simStart] Error 1
    I'm not too familiar with makefiles and this seems like a valid attempt of using extern to make cmdList1 a global variable.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe...

    Code:
    //simgroup.h
    #include <vector>
    #include "simCmd.h"
    std::vector<simCmd*> *cmdList1;
    
    //driver.C
    #include <vector>
    extern std::vector<simCmd*> *cmdList1;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    I modified it to add 'using namespace std'
    Code:
    //simgroup.h
    #include <vector>
    #include "simCmd.h"
    using namespace std;
    vector<simCmd*> *cmdList1;
    
    int main(){
      cmdList1[0] = bla bla //use of cmdList1 here
    }
    
    //driver.C
    #include <vector>
    using namespace std;
    extern vector<simCmd*> *cmdList1;
    
    I still get the error
    driver.o(.text+0x52): In function `main':
    : undefined reference to `cmdList1'
    collect2: ld returned 1 exit status
    make: *** [simStart] Error 1
    somehow it wont recognize the cmdList1 inside main

    If i add std::cmdList1 to all the parts whre cmdList1 appears I get the error
    Code:
    g++ -O2 -I/usr/include driver.C -c -o driver.o
    driver.C: In function `int main(int, char**)':
    driver.C:45: `cmdList1' undeclared in namespace `std'
    Last edited by amoskoh; 03-21-2005 at 03:55 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I just noticed something, you are compiling C source files and not C++ source files. You may need to change the extension of all your source files to whatever it needs to be (.CPP or .CC or whatever) so that the compiler understands to compile those files using C++ rules. Don't know for sure if that is part of the issue, I've never used g++.

    [edit]Also, if cmdList1 is something you've developed on your own, then it won't be in the std namespace.[/edit]
    Last edited by hk_mp5kpdw; 03-21-2005 at 04:58 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    simCmd is a class I created. Anyone with a fix yet? =(

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with linking files
    By magda3227 in forum C Programming
    Replies: 11
    Last Post: 06-18-2008, 01:00 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. extern - linking error in MSVC++ 6
    By masterlodi in forum C Programming
    Replies: 1
    Last Post: 05-28-2006, 08:55 AM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM