Thread: Is Circular Dependency a bug or a feature?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    29

    Is Circular Dependency a bug or a feature?

    I am porting some modern, usually Linux based, libraries to an archaic OS that does not have Autotools or Cmake. I've noticed a tendency to tolerate circular dependency that is detrimental to my link process.

    A module A uses a function from module B and module B is using a function from module A. Apparently, Autotools know how to resolve this without warnings. I have to do a two pass linking.

    My question is, is that a correct way to program in C? Is Circular Dependency a bug or a feature?

    Thank you
    ZA

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Function prototypes (function declarations) in a .h file included in both module .c files. A good book on the C programming language will explain this and other features you need to know especially when working with old C code.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,882
    That is not normally called a Circular Dependency.
    And, using headers in C and C++ to prototype functions is a great feature of C and C++.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    29
    But in linkage time, especially doing static link, this causes the link to fail and I need to run a two ass link
    ZA

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    424
    Quote Originally Posted by zatlas1 View Post
    A module A uses a function from module B and module B is using a function from module A. Apparently, Autotools know how to resolve this without warnings. I have to do a two pass linking.

    My question is, is that a correct way to program in C? Is Circular Dependency a bug or a feature?
    It's something you should avoid if possible, but is sometimes necessary. It's not strictly a C issue, it's a linker issue, but C allows mutal dependencies for functions.

    A case where it is necessary is if you are doing recursive descent parsing. The function parse_expression() will call the function parse_factor(), and "(expression)" is allowed as a factor. So parse_factor() has to call parse_expression().
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,882
    What Compiler/Linker are you using?

    Some linkers can list the libraries more than once to avoid the errors.
    Others have a set of linker option that tells the linker to go though the library list more than once.
    I forget which options GCC uses; might be both or neither.

    Example library listed more than once.
    Code:
    -llibA -llibB -llibA
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    What Compiler/Linker are you using?

    Some linkers can list the libraries more than once to avoid the errors.
    Others have a set of linker option that tells the linker to go though the library list more than once.
    I forget which options GCC uses; might be both or neither.
    ...
    Tim S.
    Did I say an archaic OS...

    classic z/OS on IBM z Series.

    ZA

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by zatlas1 View Post
    Did I say an archaic OS...

    classic z/OS on IBM z Series.

    ZA
    Maybe gcc for z/OS?
    The power of null.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    29
    Quote Originally Posted by Sir Galahad View Post
    Not a choice for a few reasons. I am using IBM C/C++ compiler for better or worse.
    ZA

  10. #10
    Registered User
    Join Date
    Jul 2020
    Posts
    4
    Quote Originally Posted by zatlas1 View Post
    I am porting some modern, usually Linux based, libraries to an archaic OS that does not have Autotools or Cmake. I've noticed a tendency to tolerate circular dependency that is detrimental to my link process.
    ZA
    Sorry for going off-topic from your question, but I'd really like to know what you're using in place of autotools and cmake on that system. Are you just using standard GNU make or a similar make tool? Have you looked at CDetect? I've used it with make on AIX.

    As far as circular dependencies with libraries (like with pkg-config requiring glib which requires pkg-config), I try to avoid these situations whenever possible. For instance, I replaced pkg-config with pkgconf which has no circular dependencies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circular dependency problem
    By Ducky in forum C++ Programming
    Replies: 26
    Last Post: 12-28-2014, 06:09 PM
  2. Circular Dependency A Dead End?
    By Waleed Mujeeb in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2012, 09:41 AM
  3. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  4. Replies: 3
    Last Post: 08-31-2005, 12:41 PM
  5. Circular dependency / Compile Error
    By jester in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2003, 11:17 PM

Tags for this Thread