Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "Undefined symbols" with static fields
QtWS25 Last Chance

"Undefined symbols" with static fields

Scheduled Pinned Locked Moved General and Desktop
undefined symbo
5 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xalisonx
    wrote on last edited by xalisonx
    #1

    Hi guys!

    With this code:

    #include <QString>
    #include <QDir>
    
    class Repository
    {
    public:
        static void Create() {
            BaseDirectory = Require("/Library/Application Support/myapp");
            LogDirectory = Require(BaseDirectory, "Logs");
        }
        static QString GetBaseDirectory() {
            return BaseDirectory;
        }
    private:
        static QString BaseDirectory;
        static QString LogDirectory;
    
        static QString Require(QString path){
            QDir dir(path);
            if (!dir.exists(path)) {
                dir.mkpath(path);
            }
            return path;
        }
        static QString Require(QString basePath, QString relPath){
            QString path = basePath + QDir::separator() + relPath;
            Require(path);
            return path;
        }
    };
    

    used here:

    #include "Repository.h"
    int main(int argc, char *argv[])
    {    
       Repository::Create();
    }
    

    i get this linker error:

    Undefined symbols for architecture x86_64:
      "Repository::LogDirectory", referenced from:
          Repository::Create() in main.o
      "Repository::BaseDirectory", referenced from:
          Repository::Create() in main.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    Clang on MAC!
    What's the problem??????
    :-(

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      You forgot to define the static members.
      Something like

      QString Repository::BaseDirectory;
      QString Repository::LogDirectory;
      

      in cpp file

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      X 1 Reply Last reply
      1
      • X Offline
        X Offline
        xalisonx
        wrote on last edited by
        #3

        woah you have saved my day!!!

        1 Reply Last reply
        0
        • M mcosta

          You forgot to define the static members.
          Something like

          QString Repository::BaseDirectory;
          QString Repository::LogDirectory;
          

          in cpp file

          X Offline
          X Offline
          xalisonx
          wrote on last edited by
          #4

          @mcosta is possible to define the static members in .h file?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            You have to be sure that static objects are initialized once. If you include into header file they will be initialized every time you include the header file. You can do this using special include guards but IMO is a very bad code style.

            I suggest to create a cpp file (or you can do it in main.cpp)

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved